C++ Hexadecimal Problem

Jan 16, 2009 at 2:54pm
I need help about the following:

I have a file that contains two bytes: 1B and 6A.

I am using Microsoft Visual C++ 2008. My program reads the two bytes from the file, and I want to store them in a int variable as a one number like 1B6A. How can I combine the values of the two bytes into one number? I need to turn 1B and 6A into 1B6A.

NOTE: I need a code that works with any hexadecimals from 00 to FF, not just the two I mentioned above (the code needs to work with any combinations of bytes).

Please help.
Last edited on Jan 16, 2009 at 2:58pm
Jan 16, 2009 at 4:04pm

1
2
3
4
5
6
// assume the two bytes you are reading is stored in array named "b"
        
       unsigned char b[2] = { 0x1B,0x6A};
	unsigned int var=0;	
	var = b[1];
	var = var | (b[0] << 8);
Jan 16, 2009 at 5:06pm
Thank you very much for the code. It works and I found it really useful. I sent this reply a little bit late because I had to change my code a little, so I can fit your code inside to test it. Thank you very much.
Topic archived. No new replies allowed.