string to hex number
-
Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:
char* str = "23";
char c;
sscanf(str, "%x", &c);but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,
-
Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:
char* str = "23";
char c;
sscanf(str, "%x", &c);but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,
You could only mean two things. 1. Conver the number 23 to hex. 2. Take ascii value of every character, which is again a number and convert it to hex. You mean something other than these? If yes, you are wrong.
-
Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:
char* str = "23";
char c;
sscanf(str, "%x", &c);but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,
Why don't you store it into an integer instead of a char ?
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:
char* str = "23";
char c;
sscanf(str, "%x", &c);but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,
lune12 wrote:
I have tried: char* str = "23"; char c; sscanf(str, "%x", &c); but the char c is 4 bytes in memory and this is not safe!!!
That's because
%x
indicates you want to read an integer. Read it into an integer, bounds check it if you want, then assign it to the character. If you want to ensure you only read 2 hex characters from the input, use a width specification, like this:sscanf(str, "%2x", &int_value);
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:
char* str = "23";
char c;
sscanf(str, "%x", &c);but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,
you may use
char * str = "23";
char * endptr;
char c = (char) strtol(str, &endptr, 16);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:
char* str = "23";
char c;
sscanf(str, "%x", &c);but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,
typedef unsigned char byte;
typedef wchar_t charw;
typedef const charw ccharw;ccharw *CharHexL (L"0123456789abcdef");
byte HexToByte( charw C )
{
C = towlower(C);
if( C >= CharHexL[ 0] && C <= CharHexL[ 9] ) return( byte(C-CharHexL[ 0]) );
if( C >= CharHexL[10] && C <= CharHexL[15] ) return( byte(C-CharHexL[10]) + 10 );
return(0);
}inline byte HexToByte( charw C1, charw C2 )
{
return( (HexToByte(C1)<<4) + HexToByte(C2) );
}If the string has an odd number of characters pass the first char to HexToByte(C) to get the values for the first byte. Walk the rest of the string two characters at a time using HexToByte(C1, C2) to convert the pairs to bytes.
...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack