Thread: uint8_t to uint64_t

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    uint8_t to uint64_t

    Hello

    I am trying to convert an array of 8 uint8_t to 1 uint64_t.
    But when I try to do the following it fails. Because I try to shift with 32.
    But uint64_t has 64 bits and uint8_t has only 8 so this is possible?
    So wy can't I do this?
    Code:
    int main(void){
    
    
        uint8_t bla[8];
        uint64_t temp;
        
        temp = ((bla[3] << 32) | (bla[4] << 24) | (bla[5] << 16) | (bla[6] << 8) | (bla[7]));
        printf("0x%x\n", temp);
    }
    Thank you

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I would do that with a union...

    Code:
    union t_convert
      { uint64_t bigvar;
         uint8_t  charvar[8]; }
      convert;
    Since the variables in a union overlay one another you can load your convert.charvar array with the values and read it back by accessing convert.bigvar.
    Last edited by CommonTater; 10-05-2011 at 02:49 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You would need to cast your bytes to uint64_t before trying to shift more than 32 bits.

    Eg
    (uint64_t)bla[3] << 32

    Lookup "endian" to find out why the union cast would not work on all machines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Lookup "endian" to find out why the union cast would not work on all machines.
    That's easily compensated for by the order of the values placed in the array... it's even easy to test for at run time by seeding known values and seeing what comes back... But you are correct, without logic to compensate it does become platform dependent.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Here's the easy way:
    Code:
        uint8_t bla[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
        uint64_t temp;
    
        for (int i=0; i<8; ++i)
            temp = (temp << 8) | bla[i];
    
        printf("0x%x\n", temp);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I cracked myself up with a union jack joke here, but none of you would get it, so I deleted it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uint64_t please help
    By lovesunset21 in forum C Programming
    Replies: 2
    Last Post: 12-06-2010, 02:26 PM
  2. how to mod an array of uint64_t, please help
    By lovesunset21 in forum C Programming
    Replies: 0
    Last Post: 12-06-2010, 05:00 AM
  3. printf uint64_t
    By _seven_ in forum C Programming
    Replies: 9
    Last Post: 10-08-2008, 04:56 PM
  4. 6byte array to Uint64_t
    By DavidDobson in forum C Programming
    Replies: 5
    Last Post: 09-17-2008, 12:20 PM
  5. bit shifting uint8_t
    By kdoggfunkstah in forum C++ Programming
    Replies: 8
    Last Post: 07-28-2006, 03:38 AM