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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. extract each byte from an integer varialbe

extract each byte from an integer varialbe

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
5 Posts 5 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.
  • P Offline
    P Offline
    peter ho
    wrote on last edited by
    #1

    Hi! I hope someone can help me on this. Is there a way to extract each byte from an integer variable in C++ (4 bytes) ? thanks, peter.

    D P R J 4 Replies Last reply
    0
    • P peter ho

      Hi! I hope someone can help me on this. Is there a way to extract each byte from an integer variable in C++ (4 bytes) ? thanks, peter.

      D Offline
      D Offline
      dabs
      wrote on last edited by
      #2

      There are lots of ways to do this: union ByteToInt { unsigned int i; struct bytes { unsigned char a; unsigned char b; unsigned char c; unsigned char d; } bytes; }; ByteToInt b2i; b2i.i = 0xFF00FF00; cout << b2i.bytes.a << endl; // etc. - not syntaxchecked or even compileable :-) but I hope you get the picture. Another method: unsigned int i = 0xFF00FFcc; unsigned char b1 = static_cast< unsigned char >( i ); unsigned char b2 = static_cast< unsigned char >( i >> 8 ); unsigned char b3 = static_cast< unsigned char >( i >> 16 ); unsigned char b4 = static_cast< unsigned char >( i >> 24 );


      Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!

      1 Reply Last reply
      0
      • P peter ho

        Hi! I hope someone can help me on this. Is there a way to extract each byte from an integer variable in C++ (4 bytes) ? thanks, peter.

        P Offline
        P Offline
        Paul M Watt
        wrote on last edited by
        #3

        Basically strip off the other bytes with a mask, then shift the value to fit into a BYTE. int value; BYTE b1 = (value & 0x000000FF); BYTE b2 = (value & 0x0000FF00) >> 8; BYTE b3 = (value & 0x00FF0000) >> 16; BYTE b4 = (value & 0xFF000000) >> 24;


        Build a man a fire, and he will be warm for a day
        Light a man on fire, and he will be warm for the rest of his life!

        1 Reply Last reply
        0
        • P peter ho

          Hi! I hope someone can help me on this. Is there a way to extract each byte from an integer variable in C++ (4 bytes) ? thanks, peter.

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          );
          The Ten Commandments For C Programmers

          1 Reply Last reply
          0
          • P peter ho

            Hi! I hope someone can help me on this. Is there a way to extract each byte from an integer variable in C++ (4 bytes) ? thanks, peter.

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            Perhaps the standard macros HIWORD, LOWORD, HIBYTE and LOBYTE, so you do not reinvent the wheel?    Peace! -=- James (Sonork:100.21837) "There is nothing worse than being oblivious to the fact that you do not know what you are doing." [Get Check Favorites 1.5 Now!]

            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