Hexwert aus Array ausgeben
-
Hallo, ich würde hier gerne 99 ausgegeben bekommen aber ich bekomme immer:
ffffff99 ausgegeben.Wenn ich schreibe: char s[]="\x77"; dann funktioniert alles wie ich es will dann bekomme ich eine 77 ausgeben.
Wie funktioniert das mit 99 ? Was mache ich da falsch?
#include <iostream> using namespace std; int main() { char s[]="\x99"; cout<<hex<<static_cast<int> (s[0]) <<endl; system("PAUSE"); }
-
-
@SeppJ Lies die Frage noch mal.
@TE Das liegt daran, dass die Zahl negativ wird. Nimm mal nen unsigned char.
-
edit: Ach das meinst du.
-
Danke, funktioniert:

#include <iostream> #include <sstream> using namespace std; int main() { unsigned op; unsigned char s[]="\x99"; string str; stringstream ss; ss<< hex << static_cast<unsigned>(s[0]); ss >> str; cout<<"str: " << str<<endl; stringstream ss2; ss2 << hex << str; ss2 >> op; cout<<"op: " << hex<< op<<endl; system("PAUSE"); }