Thread: concatenating two bytes into an uint16

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    61

    concatenating two bytes into an uint16

    Hi,

    I have two bytes, each in a separate array, which, when together make a 16 bit integer, i some how need to perform a logical bitwise right shift on the 'integer' and then break it up into two bytes again.

    is this even possible?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What do you mean? Make them into one and then break them up?...

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    ok, example: resp[16] = 0x5a and resp[17] = 0xcc.

    i would love to be able to printf those two array elements to one handy uint16. is this possible?

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    print to one?

    Code:
    unsigned short int x;
    x = resp[16] | (resp[17] << 8);
    ?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    sorry, i forgot to finish the example
    Code:
    resp[16] = 0x5a
    resp[17] = 0xcc
    
    <i dont know what to do>
    
    var = 0x5acc
    thankyou for you help

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    (forget the bitwise right for now...)

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    yes, read my post above.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    sorry, i got confused with my question... : s sorry.

    there is one problem, the endianness is swapped

    so

    resp[16] = 0x5a
    resp[17] = 0xcc

    x = resp[16] | (resp[17] << 8);

    -> x's value is 0xcc5a

    why is that?

    thanks

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    because you didn't specify which one would go to the higher bits before I posted that reply.

    and if you can't fix it yourself given my code... you are not worth my time.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    sorry, swapping resp[16] for resp[17] did work, when i tried it again... must have been a typo.

    thankyou for all your help, it is much appreciated

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    notice how i posted at 10:54 PM, I never read your post before i posted mine...

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    alright =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenating 2 bytes of an array
    By davo666 in forum C Programming
    Replies: 1
    Last Post: 02-28-2009, 07:37 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM