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. convert a DWORD to BYTE[4]

convert a DWORD to BYTE[4]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
10 Posts 7 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.
  • B Offline
    B Offline
    bouli
    wrote on last edited by
    #1

    Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)? DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4]; length contains the value dwLength as a 4 bytes array. Thanks :) Best regards. There is no spoon.

    PJ ArendsP M S P 4 Replies Last reply
    0
    • B bouli

      Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)? DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4]; length contains the value dwLength as a 4 bytes array. Thanks :) Best regards. There is no spoon.

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

      for (int i = 3; i >= 0; --i)
      {
      length[3 - i] = (dwLength & (0xff << (i * 8))) >> (i * 8);
      }


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


      Honoured as one of The Most Helpful Members of 2004

      Within you lies the power for good; Use it!

      1 Reply Last reply
      0
      • B bouli

        Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)? DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4]; length contains the value dwLength as a 4 bytes array. Thanks :) Best regards. There is no spoon.

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        DWORD dwLength=(DWORD) file.GetLength();
        BYTE length[sizeof(DWORD)];

        *(DWORD*) &length[0] = dwLength;

        Keep in mind that any code doing anything with the bytes will have to account for the endian type of the CPU. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

        B 1 Reply Last reply
        0
        • M Michael Dunn

          DWORD dwLength=(DWORD) file.GetLength();
          BYTE length[sizeof(DWORD)];

          *(DWORD*) &length[0] = dwLength;

          Keep in mind that any code doing anything with the bytes will have to account for the endian type of the CPU. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

          B Offline
          B Offline
          bouli
          wrote on last edited by
          #4

          Hi, Thanks :) Actually I've found the following way: length[0]=HIBYTE(HIWORD(dwLength)); length[1]=LOBYTE(HIWORD(dwLength)); length[2]=HIBYTE(LOWORD(dwLength)); length[3]=LOBYTE(LOWORD(dwLength)); which also works :) There is no spoon.

          M M 2 Replies Last reply
          0
          • B bouli

            Hi, Thanks :) Actually I've found the following way: length[0]=HIBYTE(HIWORD(dwLength)); length[1]=LOBYTE(HIWORD(dwLength)); length[2]=HIBYTE(LOWORD(dwLength)); length[3]=LOBYTE(LOWORD(dwLength)); which also works :) There is no spoon.

            M Offline
            M Offline
            Mike_V
            wrote on last edited by
            #5

            Or how about: BYTE *pLength = (BYTE*)dwLength; pLength[0] will equal the low byte (assuming an intel machine) What do you need this for?

            1 Reply Last reply
            0
            • B bouli

              Hi, Thanks :) Actually I've found the following way: length[0]=HIBYTE(HIWORD(dwLength)); length[1]=LOBYTE(HIWORD(dwLength)); length[2]=HIBYTE(LOWORD(dwLength)); length[3]=LOBYTE(LOWORD(dwLength)); which also works :) There is no spoon.

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              It works, but not the same way as my code in my earlier post. Yours forces big-endian order. Which isn't wrong per se, it's just something to be aware of. Don't try to write that byte array out to a file and read it back in as a DWORD, for example. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

              1 Reply Last reply
              0
              • B bouli

                Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)? DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4]; length contains the value dwLength as a 4 bytes array. Thanks :) Best regards. There is no spoon.

                S Offline
                S Offline
                softwero
                wrote on last edited by
                #7

                Hi DWORD dwData=0x65A6;//any value BYTE arrDest[4]; memcpy(arrDest,&dwData,sizeof(DWORD)); :)

                1 Reply Last reply
                0
                • B bouli

                  Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)? DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4]; length contains the value dwLength as a 4 bytes array. Thanks :) Best regards. There is no spoon.

                  P Offline
                  P Offline
                  Priyank Bolia
                  wrote on last edited by
                  #8

                  I have one more method, but not good. union {       DWORD dwNumber;       BYTE byArry[4]; } http://www.priyank.in/

                  D 1 Reply Last reply
                  0
                  • P Priyank Bolia

                    I have one more method, but not good. union {       DWORD dwNumber;       BYTE byArry[4]; } http://www.priyank.in/

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Priyank Bolia wrote: ...but not good Why? This is the method I've used in years past. I find it easier to read than a bunch of pointers ands casts.


                    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                    P 1 Reply Last reply
                    0
                    • D David Crow

                      Priyank Bolia wrote: ...but not good Why? This is the method I've used in years past. I find it easier to read than a bunch of pointers ands casts.


                      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                      P Offline
                      P Offline
                      Priyank Bolia
                      wrote on last edited by
                      #10

                      I just write that thinking that people like it or not. I still use it in imaging programs, where image documents are large size and memory and speed are a constraint. It saves memory and helps out small programmers from the pointers nightmare, and the main point is it will save precious time as compare to for loops and memcpy operations etc. http://www.priyank.in/

                      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