Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I combine two hex values into one integer value?

How can I combine two hex values into one integer value?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++sharepointdata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TylerD75
    wrote on last edited by
    #1

    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

    T W 2 Replies Last reply
    0
    • T 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, TylerD75

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      Use the bitwise shift operators

      short x = 0;
      x = 0x5A;
      x ^= 0x4D << 8;

      T 1 Reply Last reply
      0
      • T 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, TylerD75

        T Offline
        T Offline
        TylerD75
        wrote on last edited by
        #3

        Never mind! I figured out how to do it with memcpy ;) Cheers, TylerD75

        1 Reply Last reply
        0
        • W Waldermort

          Use the bitwise shift operators

          short x = 0;
          x = 0x5A;
          x ^= 0x4D << 8;

          T Offline
          T Offline
          TylerD75
          wrote on last edited by
          #4

          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

          W 1 Reply Last reply
          0
          • T TylerD75

            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

            W Offline
            W Offline
            Waldermort
            wrote on last edited by
            #5

            no problem. It can be done in one line, but I simplified for you.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups