splitting 'unsigned long long int'(uint64_t) to 8 bytes(int8_t).

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coral99
    New Member
    • Feb 2010
    • 1

    splitting 'unsigned long long int'(uint64_t) to 8 bytes(int8_t).

    Hi,
    I'm writing an application that receives data(ASCII characters) serially via wiegand interface into a variable uint64_t mydata. I need to split mydata into 8 bytes which represents individual characters.

    Can somebody help me with a way to split/convert uint64_t into 8 bytes.

    Thanks you in advance.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Can't you just typecast a pointer to this uint64_t to a unsigned char?

    That way you could use the uint_64_t as an unsigned char array[8].

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      While that method works on many many platforms (well all the diffeent ones I have used) the standard does not guaranttee that the conversion of any pointer to any other pointer except for pointer to type to pointer to void or casting up and down a class heiarchy is valid.

      Therefore casting like that is not strictly portable although it is very likely to work.

      A portable (but slower) method would be to use the bitwise operators (<< >> &) to isolate each individual byte in the 64 bit value.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Also, using the bitwise shift makes your code endian-neutral.

        Comment

        Working...