Skip to main content
gamedev.net
🔒 Locked

Function for return hexadecimal code?

Started by pepeland Aug 17, 2005 at 10:03 PM 17 replies 12.2k views
Original Post
pepeland
pepeland
Hi I want a fuction in C or C++ that get a character and return hexadecimal code. Thanks...
Hamid.R Gharahzadehwww.pepeland.net
dave
dave
You could use good old sprintf() probably.

Not typesafe but it will do. Make use of the "%x" to format to hex i believe.

ace
izhbq412
izhbq412
A character in C++ is its hex code. If you want a string that represents the ascii code of the character you can use the sprintf function, as ace_lovegrove suggested:
std::string CharToHex( char ch ){    char buf[5];    sprintf( buf, "0x%x", ch );    return buf;}


It's totally safe :)
___The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
Sharlin
Sharlin
std::string CharToHex( char ch ){    char buf[5] = {};    sprintf( buf, "0x%02x", ch & 0xFF );    return buf;}


Actually, now it is quite safe. Had C++ snprintf, it could (and should!) have been used, just to be sure.
FlowingOoze
FlowingOoze
Why this initialization?
Quote:
Original post by Sharlin
...    char buf[5] = {};...



Anyway, Boost Format does the trick quite nicely:
std::string CharToHex( char ch ){  return boost::str(boost::format("%x") % ch);}

Sharlin
Sharlin
Double redundancy "just in case" :P
Probably unnecessary, but a little paranoia is never a bad thing when it comes to the C library.
ToohrVyk
ToohrVyk
std::string CharToHex( char ch ){    const int size = 5;    char buf[size] = {};    verify( size > sprintf( buf, "0x%02x", ch & 0xFF ));    return buf;}


The above is better, actually, since:
1° It explicitly states what the maximum size that can be written into the buffer, thus reminiding maintenance programmers that it is hazardous ot change the format string.
2° It does not use "magic" literals that determine the size of the buffer: the literal only appears in one place, and its named value is used elsewhere.
3° Should someone change the format of the sprintf call without adapting the buffer size, an assert would usually fail when the buffer is overwritten.

Besides, I cannot remember if sizeof("0xFF") is legal or not, but if it is, it should be used in place of the "5".
Sharlin
Sharlin
What about this :)

std::string CharToHex( char ch ){    char buf[] = "0xFF";    verify( sizeof(buf) > sprintf( buf, "0x%02x", ch & 0xFF ));    return buf;}

Quote:
Besides, I cannot remember if sizeof("0xFF") is legal or not, but if it is, it should be used in place of the "5".

It is, but it returns sizeof(const char*).
ToohrVyk
ToohrVyk
Nice :)

Quote:
Original post by Sharlin
Quote:
Besides, I cannot remember if sizeof("0xFF") is legal or not, but if it is, it should be used in place of the "5".

It is, but it returns sizeof(const char*).


GCC seems to return 5, not 4.
Sharlin
Sharlin
Ah, indeed, I stand corrected. Quoth the standard:
Quote:

An ordinary string literal has type "array of n const char" and static storage duration (_basic.stc_), where n is the size of the string as defined below, and is initialized with the given characters.

(emphasis mine)
FlowingOoze
FlowingOoze
Quote:
Original post by Anonymous Poster
Lets try that again....
one single-threaded safe func:

code removed

should be loads faster and safer :)


Too bad that's not standard C or C++. Oh, and I wouldn't call it safe.

[Hint: The standard doesn't specify character sets. And never return a pointer to a local variable.]
Zahlman
Zahlman
If you just want to output the value as hex, look into stream manipulators.

You could do it with stream manipulators anyway if you want to save the new string:

std::string toHex(char c) {  std::stringstream ss;  ss << std::hex << c;  return ss.str();}


But that's rather overkill. Let's build the string "manually", but still use an actual string, and have simple logic:

std::string toHex(char c) {  static const char symbols[16] = {     '0', '1', '2', '3', '4', '5', '6', '7',     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; // we don't want a trailing \0  std::string result(2, '\0');  result[0] = symbols[c >> 4];  result[1] = symbols[c & 15];  return result;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.