Jump to content
  • Advertisement
Sign in to follow this  
pepeland

Function for return hexadecimal code?

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
Double redundancy "just in case" :P
Probably unnecessary, but a little paranoia is never a bad thing when it comes to the C library.

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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*).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
one single-threaded safe func:

const char* CharToHex( const char C )
{
char result[3];
result[0] = '0' +((C>>4)&15);
if( '9' <

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!