Thread: uint16_t to two uint_8 values?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    uint16_t to two uint_8 values?

    Hi Everyone

    I have a single uint_16 value that I need to break down into two separate uint8_t values so I can store them in a char[]. I intend to reform the two values at a later date and get the uint16_t value I had originally.

    Can someone help me out? I have no idea how to go about doing this :S

    Thank you very much

    Code:
    uint16_t value = 0xFFFF;
    
    uint8_t partA;
    uint8_t partB;
    
    ?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You'll need to use bit shifting and bit masking. Here a resource on the subject: http://www.cprogramming.com/tutorial...operators.html

    Code:
    uint16_t value = 0xFFFF;
    
    uint8_t partA = (uint8_t)((value & 0xFF00) >> 8);
    uint8_t partB = (uint8_t)(value & 0x00FF);
    gg

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Alternatively,
    Code:
    union uint16_or_8
    {
         uint16_t value;
         int8_t  parts[2];
    } variable_name;
    I'm not sure why you need to break them apart to store a representation in a string. Why not just the value to the string with appropriate formatting?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The union idea is very bad, it breaks on machines with the wrong endian.
    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.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by Codeplug View Post
    You'll need to use bit shifting and bit masking. Here a resource on the subject: http://www.cprogramming.com/tutorial...operators.html

    Code:
    uint16_t value = 0xFFFF;
    
    uint8_t partA = (uint8_t)((value & 0xFF00) >> 8);
    uint8_t partB = (uint8_t)(value & 0x00FF);
    gg
    I would have thought that theres no need to mask partA.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I would have thought that theres no need to mask partA
    Perhaps not - but it shows intention, making it a little more "self-documenting". And hopefully the compiler would generate the same code either way.

    gg

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Codeplug View Post
    >> I would have thought that theres no need to mask partA
    Perhaps not - but it shows intention, making it a little more "self-documenting". And hopefully the compiler would generate the same code either way.

    gg
    Most likely, yes.

    I'd like to suggest that you shift and then mask, not mask then shift. When you mask and shift, both the mask and the shift vary for each position -- when you shift and mask, only the shift quantity varies, while the mask stays the same each time. Makes it a bit easier to code.

    Also, a slew of other reasons I'm having a hard time putting into words.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM