(C++) Get the byte-values from an unsigned int32

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phiber4591
    New Member
    • Jan 2009
    • 1

    (C++) Get the byte-values from an unsigned int32

    Hi there,

    I'm trying to "convert" an unsigned int32 to an array of bytes (with a length of 4, of course). Everything i tried up to now didn't work out, so I would really appreciate any help or tip ;-)

    My idea is that an uint32 has a length of 4 bytes, and thus it should be possible to take these bytes on their own and do something with them.

    Short example:
    uint32 contains the (dec) value 439041101, which is in hex 1A2B3C4D.

    All I want is an array of 4 char's or bytes with the values:
    ret[0]: 1A
    ret[1]: 2B
    ret[2]: 3C
    ret[3]: 4F
    (hex-value [and not a string])


    Many thanks in advance,

    phiber
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Here's 2 methods:

    1. Take a pointer to your integer and cast it to a pointer to unsigned char, not portable but does work on most systems today.

    2. Create a union of 1 int and an array of 4 bytes, assign you int to an instance of the union and then read out the byte array.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Banfa
      Here's 2 methods:

      1. Take a pointer to your integer and cast it to a pointer to unsigned char, not portable but does work on most systems today.

      2. Create a union of 1 int and an array of 4 bytes, assign you int to an instance of the union and then read out the byte array.
      Both these methods fail on a little endian machine but the htonl macro can be of help then.

      kind regards,

      Jos

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by JosAH
        Both these methods fail on a little endian machine but the htonl macro can be of help then.
        I think what you mean is both those methods have to take the endian of the machine into account when copying the data out into your array and are therefore non-portable.

        On the other hand htonl is part of a third party library that is not available on all platforms and there for also non-portable.

        The portable method is to use the bitwise right shift and and operators to extract the byte values.

        :D

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Banfa
          On the other hand htonl is part of a third party library that is not available on all platforms and there for also non-portable.

          The portable method is to use the bitwise right shift and and operators to extract the byte values.

          :D
          IMHO that borders to paranoia; if a platform even smells like POSIX it has those functions/macros available.

          kind regards,

          Jos

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            True but 90% - 100% of the reasonable number of platforms I have worked on have not smelt like POSIX at all.

            Comment

            Working...