Advertisement Jump to content


This topic is now archived and is closed to further replies.


Converting a byte to hex code?

This topic is 6547 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

I''d like to convert a byte in memory to a hex code string. Anyone know how I can do this easily? (I know almost nothing about hex) Many thanks Chris

Share this post

Link to post
Share on other sites
There is a function for converting between numbers and strings and different bases. It is itoa(). It converts an integer to a string. You specify your byte and a pointer to memory and the base you want, here 16, and the function will return that byte as a string, in base 16. Here is a cutout of the example code given in the VC++ 6.0 library. Hope this helps:


void main( void )
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;

_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s\n", i, buffer );

_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );

_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );

_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s\n", l,
buffer );

_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
buffer );


String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2

Share this post

Link to post
Share on other sites
Assuming you just want it converted to a string for display purposes, printf does what you need:

char cVal = 69;
printf("%0x\n", cVal);

Share this post

Link to post
Share on other sites
Cool... thanks all, I went with Jon''s solution simple because I was outputting the text anyway to stdout.

in case anyone every wants to convert an image to hex code to include in a header here some really basic code:

#include <vector>
#include <fstream.h>

void main(void)
std::vector<unsigned char> Array;

ifstream file("default.raw", ios::in | ios::binary | ios::nocreate);
if (
//Get the files size

file.seekg( 0, ios::end );
unsigned long FileSize = file.tellg();
file.seekg( 0, ios::beg );

//Resize the memory container

Array.resize(FileSize); Array.begin(), FileSize );


for (unsigned long i=0; i<Array.size()/8; i++)
for (unsigned long j=0; j<8; j++)
printf("0x%0x, ", Array[i+j]);

not pretty and you have to drop the last comma but it gets the job done.

I encoded a 192k greyscale font bitmap to hex and included it in my project. The data increased the file by about 60k, not too bad really...(Release mode)

Many thanks to all, no my console will never be without a font...


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!