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. Splitting an Integer into 4 Bytes

Splitting an Integer into 4 Bytes

Scheduled Pinned Locked Moved C / C++ / MFC
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.
  • S Offline
    S Offline
    sschilachi
    wrote on last edited by
    #1

    how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this. int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i); Can anyone help? Thanks

    P R G 3 Replies Last reply
    0
    • S sschilachi

      how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this. int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i); Can anyone help? Thanks

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      Many way to do this. One brute force way is:

      b1 = i & 0x000000ff;
      b2 = (i & 0x0000ff00) >> 8;
      b3 = (i & 0x00ff0000) >> 16;
      b3 = (i & 0xff000000) >> 24;


      [

      ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

      1 Reply Last reply
      0
      • S sschilachi

        how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this. int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i); Can anyone help? Thanks

        R Offline
        R Offline
        Roman Nurik
        wrote on last edited by
        #3

        not a very safe-cast method but who cares it works: int i = 12345; unsigned char byte1 = ((unsigned char*)&i)[0]; unsigned char byte2 = ((unsigned char*)&i)[1]; unsigned char byte3 = ((unsigned char*)&i)[2]; unsigned char byte4 = ((unsigned char*)&i)[3];

        r -€

        1 Reply Last reply
        0
        • S sschilachi

          how do you split an integer variable into 4 byte variables, I'm looking for a function to achieve this. int i; byte b1,b2,b3,b4; b1 = Byte1(i); b2 = Byte2(i); b3 = Byte3(i); b4 = Byte4(i); Can anyone help? Thanks

          G Offline
          G Offline
          grigsoft
          wrote on last edited by
          #4

          I will add this one: int i=12345; BYTE* ab = (BYTE*)&i; // use ab[0-3] Igor Green http://www.grigsoft.com/ - files and folders comparison tools

          P 1 Reply Last reply
          0
          • G grigsoft

            I will add this one: int i=12345; BYTE* ab = (BYTE*)&i; // use ab[0-3] Igor Green http://www.grigsoft.com/ - files and folders comparison tools

            P Offline
            P Offline
            Prakash Nadar
            wrote on last edited by
            #5

            assume i is 2 bytes for simplicity. int i = 0x1234; byte c1,c2, then according to your logic, the output would be like this. c1 = 0x34; and c2 = 0x12; which is wrong.This is because the way it is stored in the memory. and if you consider for 32bit integer value, the error would just double. and according to PJ c1 = 0x12; & c2 = 0x34; here the output is correct, hope you got the picture 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