Leo hat geschrieben:Für alle möglichen Umwandlungen von Zahl zu String und
String zu Zahl gibt es diese Funktion, die aber auch mit
stringstreams arbeitet:
Für BASIC-Coder, die Str() und Hex() gewohnt sind, hier
noch bissl was zum rumprobieren und rumspielen:
Code: Alles auswählen
/*
*
* Str(), Hex(), Oct(), Val()
*
*/
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <string>
using namespace std;
template<class T>
std::string Str(T x) {
std::ostringstream stream;
stream << x;
return stream.str();
}
template<class T>
std::string Str(T x, int length, char fill=' ', bool left=false) {
std::ostringstream stream;
if(left) stream << setfill(fill) << setw(length) << std::left << x;
else stream << setfill(fill) << setw(length) << x;
return stream.str();
}
template<class T>
std::string Hex(T x) {
std::ostringstream stream;
stream << std::hex << std::uppercase << x;
return stream.str();
}
template<class T>
std::string Hex(T x, int length, char fill=' ', bool left=false) {
std::ostringstream stream;
if(left) stream << setfill(fill) << setw(length) << std::hex
<< std::uppercase << std::left << x;
else stream << setfill(fill) << setw(length) << std::hex
<< std::uppercase << x;
return stream.str();
}
template<class T>
std::string Oct(T x) {
std::ostringstream stream;
stream << std::oct << x;
return stream.str();
}
template<class T>
std::string Oct(T x, int length, char fill=' ', bool left=false) {
std::ostringstream stream;
if(left) stream << setfill(fill) << setw(length) << std::oct << std::left << x;
else stream << setfill(fill) << setw(length) << std::oct << x;
return stream.str();
}
template<class T>
T Val(std::string s) {
T a;
std::stringstream stream;
stream << s;
stream >> a;
return a;
}
std::string Chr(unsigned char x) {
std::ostringstream stream;
stream << x;
return stream.str();
}
int main(int argc, char *argv[])
{
int a = 12;
float b = 12.34f;
unsigned char c = 65;
long d = -1234567;
unsigned long e = 0xFFFFFFFF;
long f = 0xFFFFFFFF;
// long long g = 0x7FFFFFFFFFFFFFFF;
// unsigned long long h = 0xFFFFFFFFFFFFFFFF;
cout << "Dezimal: " << Str(a,10) << " Hex: " << Hex(a,8) << endl
<< "Dezimal: " << Str(b,10) << " Hex: " << Hex(b,8) << endl
<< "Dezimal: " << Str(c,10) << " Hex: " << Hex(c,8) << endl
<< "Dezimal: " << Str(d,10) << " Hex: " << Hex(d,8) << endl
<< "Dezimal: " << Str(e,10) << " Hex: " << Hex(e,8) << endl
<< "Dezimal: " << Str(f,10) << " Hex: " << Hex(f,8) << endl;
//<< "Dezimal: " << Str(g,20) << " Hex: " << Hex(g,8) << endl
//<< "Dezimal: " << Str(h,20) << " Hex: " << Hex(h,8) << endl;
cout << endl;
// string von datentyp erstellen
string s = Str(a) + " -- " + Str(b);
cout << "string: " << s << endl << endl;
a=255;
cout << Oct(a) << endl;
int x = Val<int>("-123");
cout << x << endl;
float y = Val<float>("123.456");
cout << y << endl;
for(long i=0; i<256; i++) cout << Chr(i);
system("PAUSE");
return 0;
}
Code: Alles auswählen
/*
* ToString() und ToHex() Tests als Template Klasse
*
* Ausgabe:
*
* Dezimal: 10 Hex: A
* Dezimal: 12.34 Hex: 12.34
* Dezimal: A Hex: A
* Dezimal: -1234567 Hex: FFED2979
* Dezimal: 4294967295 Hex: FFFFFFFF
* Dezimal: -1 Hex: FFFFFFFF
* Dezimal: 9223372036854775807 Hex: 7FFFFFFFFFFFFFFF
* Dezimal: 18446744073709551615 Hex: FFFFFFFFFFFFFFFF
*
* string: 10 -- 12.34
*
*/
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <string>
using namespace std;
template<class T>
class var {
public:
var() { value=0; }
var(T a) { value=a; }
/*
friend ostream &operator<<(ostream &stream, T &o) {
return stream << o.value;
}
friend istream &operator>>(istream &stream, T &o) {
return stream >> o.value;
}
*/
std::string ToString() {
std::ostringstream stream;
stream << this->value;
return stream.str();
}
std::string ToString(int length, char fill=' ', bool left=false) {
std::ostringstream stream;
if(left) stream << setfill(fill) << setw(length) << std::left << value;
else stream << setfill(fill) << setw(length) << value;
return(stream.str());
}
std::string ToHex() {
std::ostringstream stream;
stream << hex << uppercase << value;
return stream.str();
}
std::string ToHex(int length, char fill=' ', bool left=false) {
std::ostringstream stream;
if(left) stream << setfill(fill) << setw(length) << std::hex
<< std::uppercase << std::left << value;
else stream << setfill(fill) << setw(length) << std::hex
<< std::uppercase << value;
return(stream.str());
}
operator std::string() {
return this->ToString();
}
operator T() { return value; }
T operator++() { value++; return *this; }
T operator++(int a) { value++; return *this; }
T operator--() { value--; return *this; }
T operator--(int a) { value--; return *this; }
T operator+=(int a) { value += a; return *this; }
T operator-=(int a) { value -= a; return *this; }
private:
T value;
};
template<class T>
void Ausgabe(T x) {
cout << "Dezimal: "
<< x.ToString(20)
<< " Hex: "
<< x.ToHex(16)
<< endl;
}
int main(int argc, char *argv[])
{
var<int> a = 12;
var<float> b = 12.34f;
var<char> c = 65;
var<long> d = -1234567;
var<unsigned long> e = 0xFFFFFFFF;
var<long> f = 0xFFFFFFFF;
// var<long long> g = 0x7FFFFFFFFFFFFFFF;
// var<unsigned long long> h = 0xFFFFFFFFFFFFFFFF;
a++; // teste operatoren ++
++a; //
a--; // teste operatoren --
--a; //
a += 1; // 12 + 1 = 13
a -= 3; // 13 - 3 = 10
Ausgabe(a);
Ausgabe(b);
Ausgabe(c);
Ausgabe(d);
Ausgabe(e);
Ausgabe(f);
// Ausgabe(g);
// Ausgabe(h);
cout << endl;
// string von datentyp erstellen
string s = a.ToString() + " -- " + b.ToString();
cout << "string: " << s << endl << endl;
system("PAUSE");
return 0;
}
Code: Alles auswählen
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Integer {
public:
Integer() { value=0; }
Integer(int a) { value=a; }
friend ostream &operator<<(ostream &stream, Integer &o) {
return stream << o.value;
}
friend istream &operator>>(istream &stream, Integer &o) {
return stream >> o.value;
}
operator string() {
std::stringstream stream;
stream << value;
return stream.str();
}
operator int() { return value; }
Integer operator++() { value++; return *this; }
Integer operator++(int a) { value++; return *this; }
Integer operator--() { value--; return *this; }
Integer operator--(int a) { value--; return *this; }
Integer operator+=(int a) { value += a; return *this; }
Integer operator-=(int a) { value -= a; return *this; }
private:
int value;
};
int main(int argc, char *argv[])
{
Integer a = 12;
a++; // teste operatoren ++
++a; //
a--; // teste operatoren --
--a; //
a += 1; // 12 + 1 = 13
a -= 3; // 13 - 3 = 10
cout << a << endl
<< "Bitte geben sie eine Integer-Zahl ein: ";
cin >> a;
cout << a << endl;
string x = a;
cout << "Inhalt von String 'x': " << x << endl;
system("PAUSE");
return 0;
}
Wie gesagt, nur zum rumspielen für Umsteiger gedacht...
