How to convert hex to string?
-
Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like:
string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); }
This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". Rostfrei -
Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like:
string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); }
This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". RostfreiRostfrei wrote:
char a[3] = {0};
You have to increase the size of the array(may be something like a[10], this will be much safer).
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:
-
Rostfrei wrote:
char a[3] = {0};
You have to increase the size of the array(may be something like a[10], this will be much safer).
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:
-
7f ff 1a. I am getting this as output.
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:
-
7f ff 1a. I am getting this as output.
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:
-
Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like:
string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); }
This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". RostfreiRostfrei wrote:
char buffer2[100] = {0x7f, 0x80, 0x1a};
Here is the problem. Make that
unsigned char buffer2[100];
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:
-
Rostfrei wrote:
char buffer2[100] = {0x7f, 0x80, 0x1a};
Here is the problem. Make that
unsigned char buffer2[100];
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:
-
Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like:
string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); }
This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". RostfreiIn my oppinion there is way too much
printf
ing in C++ code. Why not something like this?#include <sstream> #include <iomanip> string print_buff; BYTE buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++) { ostringstream ss; ss << setw(2) << setfill('0') << hex << buffer2[i]; print_buff += ss.str(); }
Steve -
In my oppinion there is way too much
printf
ing in C++ code. Why not something like this?#include <sstream> #include <iomanip> string print_buff; BYTE buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++) { ostringstream ss; ss << setw(2) << setfill('0') << hex << buffer2[i]; print_buff += ss.str(); }
SteveGame over ;P :jig:
Jesus Lives Forever - Amen:rose:
--Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:
-
Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like:
string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); }
This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". RostfreiIn most C++ compilers,
char
is a signed data type. This means that values greater than or equal to0x80
interpret the high bit as a sign bit. You are using an integer format%2.0x
, so it extends the sign bit. Try this:string print_buff;
char buffer2[100] = {0x7f, 0x80, 0x1a};
for(int i = 0; i < 3; i++){
char a[3] = {0};
sprintf(a, "%02x", **(unsigned)**buffer2[i]);
print_buff.append(a, 2);
}The
(unsigned)
cast converts the character to anunsigned int
. I also changed the format slightly.
Software Zen:
delete this;
-
In my oppinion there is way too much
printf
ing in C++ code. Why not something like this?#include <sstream> #include <iomanip> string print_buff; BYTE buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++) { ostringstream ss; ss << setw(2) << setfill('0') << hex << buffer2[i]; print_buff += ss.str(); }
SteveHello! By looking at the code I know it should work, but it just doesn't and I don't know why. I have an array of hexadecimal numbers (char) 00 1C 61 1A 80 02 07 80 A1 08 06 06 2B 0C 00 81 34 01 A2 03 02 01 00 A3 05 A1 03 02 01 01 when I try to convert it to string the result in print_buff is only "0". Rostfrei