How can I combine two hex values into one integer value?
-
I've got two HEX values: 0x4D and 0x5A I want to combine them (not add them together), so that I have an unsigned short variable with the value 0x4D5A (= 19802). How can I accomplish this? To be a little bit more specific: 1. I've got a temp array (
unsigned char tmp[1];
) where each byte in a binary stream passes through.tmp[0] = br->ReadByte(); tmp[1] = br->ReadByte();
2. I've made a struct like this:struct EXE { unsigned short signature; unsigned short bytes_in_last_block; unsigned short blocks_in_file; unsigned short num_relocs; unsigned short header_paragraphs; unsigned short min_extra_paragraphs; unsigned short max_extra_paragraphs; unsigned short ss; unsigned short sp; unsigned short checksum; unsigned short ip; unsigned short cs; unsigned short reloc_table_offset; unsigned short overlay_number; void dbgPrintToConsole() { Console::WriteLine(L"Signature - HEX : 0x{0:X}", signature); Console::WriteLine(L"Bytes in last block : {0}", bytes_in_last_block); Console::WriteLine(L"Number of blocks in file : {0}", blocks_in_file); Console::WriteLine(L"Number of Relocations : {0}", num_relocs); Console::WriteLine(L"Header Paragraphs : {0}", header_paragraphs); Console::WriteLine(L"Minimum Extra Paragraphs : {0}", min_extra_paragraphs); Console::WriteLine(L"Maximum Extra Paragraphs : {0}", max_extra_paragraphs); Console::WriteLine(L"SS register start-value : {0:X}", ss); Console::WriteLine(L"SP register start-value : {0:X}", sp); Console::WriteLine(L"Checksum (usually not used): {0:X}", checksum); Console::WriteLine(L"IP register start-value : {0:X}", ip); Console::WriteLine(L"CS register start-value : {0:X}", cs); Console::WriteLine(L"Relocation table offset : {0:X}", reloc_table_offset); Console::WriteLine(L"Overlay Number : {0}", overlay_number); } };
So what I'd like to do, is to assign every two bytes into the unsigned short variables in the struct EXE. I should probably mention that I am a VC++ newbee. Hope this makes sense to someone? Cheers, TylerD75 -
I've got two HEX values: 0x4D and 0x5A I want to combine them (not add them together), so that I have an unsigned short variable with the value 0x4D5A (= 19802). How can I accomplish this? To be a little bit more specific: 1. I've got a temp array (
unsigned char tmp[1];
) where each byte in a binary stream passes through.tmp[0] = br->ReadByte(); tmp[1] = br->ReadByte();
2. I've made a struct like this:struct EXE { unsigned short signature; unsigned short bytes_in_last_block; unsigned short blocks_in_file; unsigned short num_relocs; unsigned short header_paragraphs; unsigned short min_extra_paragraphs; unsigned short max_extra_paragraphs; unsigned short ss; unsigned short sp; unsigned short checksum; unsigned short ip; unsigned short cs; unsigned short reloc_table_offset; unsigned short overlay_number; void dbgPrintToConsole() { Console::WriteLine(L"Signature - HEX : 0x{0:X}", signature); Console::WriteLine(L"Bytes in last block : {0}", bytes_in_last_block); Console::WriteLine(L"Number of blocks in file : {0}", blocks_in_file); Console::WriteLine(L"Number of Relocations : {0}", num_relocs); Console::WriteLine(L"Header Paragraphs : {0}", header_paragraphs); Console::WriteLine(L"Minimum Extra Paragraphs : {0}", min_extra_paragraphs); Console::WriteLine(L"Maximum Extra Paragraphs : {0}", max_extra_paragraphs); Console::WriteLine(L"SS register start-value : {0:X}", ss); Console::WriteLine(L"SP register start-value : {0:X}", sp); Console::WriteLine(L"Checksum (usually not used): {0:X}", checksum); Console::WriteLine(L"IP register start-value : {0:X}", ip); Console::WriteLine(L"CS register start-value : {0:X}", cs); Console::WriteLine(L"Relocation table offset : {0:X}", reloc_table_offset); Console::WriteLine(L"Overlay Number : {0}", overlay_number); } };
So what I'd like to do, is to assign every two bytes into the unsigned short variables in the struct EXE. I should probably mention that I am a VC++ newbee. Hope this makes sense to someone? Cheers, TylerD75Use the bitwise shift operators
short x = 0;
x = 0x5A;
x ^= 0x4D << 8; -
I've got two HEX values: 0x4D and 0x5A I want to combine them (not add them together), so that I have an unsigned short variable with the value 0x4D5A (= 19802). How can I accomplish this? To be a little bit more specific: 1. I've got a temp array (
unsigned char tmp[1];
) where each byte in a binary stream passes through.tmp[0] = br->ReadByte(); tmp[1] = br->ReadByte();
2. I've made a struct like this:struct EXE { unsigned short signature; unsigned short bytes_in_last_block; unsigned short blocks_in_file; unsigned short num_relocs; unsigned short header_paragraphs; unsigned short min_extra_paragraphs; unsigned short max_extra_paragraphs; unsigned short ss; unsigned short sp; unsigned short checksum; unsigned short ip; unsigned short cs; unsigned short reloc_table_offset; unsigned short overlay_number; void dbgPrintToConsole() { Console::WriteLine(L"Signature - HEX : 0x{0:X}", signature); Console::WriteLine(L"Bytes in last block : {0}", bytes_in_last_block); Console::WriteLine(L"Number of blocks in file : {0}", blocks_in_file); Console::WriteLine(L"Number of Relocations : {0}", num_relocs); Console::WriteLine(L"Header Paragraphs : {0}", header_paragraphs); Console::WriteLine(L"Minimum Extra Paragraphs : {0}", min_extra_paragraphs); Console::WriteLine(L"Maximum Extra Paragraphs : {0}", max_extra_paragraphs); Console::WriteLine(L"SS register start-value : {0:X}", ss); Console::WriteLine(L"SP register start-value : {0:X}", sp); Console::WriteLine(L"Checksum (usually not used): {0:X}", checksum); Console::WriteLine(L"IP register start-value : {0:X}", ip); Console::WriteLine(L"CS register start-value : {0:X}", cs); Console::WriteLine(L"Relocation table offset : {0:X}", reloc_table_offset); Console::WriteLine(L"Overlay Number : {0}", overlay_number); } };
So what I'd like to do, is to assign every two bytes into the unsigned short variables in the struct EXE. I should probably mention that I am a VC++ newbee. Hope this makes sense to someone? Cheers, TylerD75 -
Use the bitwise shift operators
short x = 0;
x = 0x5A;
x ^= 0x4D << 8; -
Hey thanks m8! That worked too! I'll implement a method in struct EXE, with a switch, so your way is A LOT simpler! Thanks again! Cheers, TylerD
no problem. It can be done in one line, but I simplified for you.