uint16_t to two uint_8 values?

This is a discussion on uint16_t to two uint_8 values? within the C Programming forums, part of the General Programming Boards category; Hi Everyone I have a single uint_16 value that I need to break down into two separate uint8_t values so ...

  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,607
    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
    5,830
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,607
    >> 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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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, 09: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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21