Thread: Splitting up int16_t to two int8_t

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    Splitting up int16_t to two int8_t

    I try to figure out how to split up one int16_t into two int8_t... But I can't figure it out.

    Well actually, I have a string containing some stuff. Then I have a representation of two chars in an int16_t that I would like to paste into a certain place of the string. How can I do this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try looking at what x % 256 and x / 256 give you.
    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.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I was thinking about bit shifting left* or right 8bits to move the unwanted char out of the space allocated for the int 16_t.. but then I think you'd have to typecast the 16_t variable to an 8_t ....

    would this be a viable method..? if not .. why??






    Code:
    footnotes:
    
    * shifting left would require additional manipulation in order to occupy the lower 8-bits of the 16_t var.
    
     example: 
    
     8 << var;    //move out upper 8 bits
    
    then 
    
     8 >> var;  //return the char back to lower 8
    Last edited by The Brain; 01-26-2005 at 12:55 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > would this be a viable method..? if not .. why?
    It's OK so long as you are aware that >> on signed numbers is implementation specific.
    So long as you take this into account, it's a good way to do it.
    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
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    thanks for your answer salem (forgot about the sign bit!)



    oh.. I just thought of something.. an even better way i think.. would be just to "and" the 16_t variable with a mask

    pseudocode:
    Code:
    16_t before;
    
    11111111 00000000b & before;    // "and" using this mask to clear out the lower 8 bits
    
    before >> 8;   //shift the upper bite right to occupy the lower 8 bits
    
    8_t after_1 = static_cast<8_t>(var);   //the "upper" char is returned    :)


    For extra credit: How would you perform similar operations for a signed char
    Last edited by The Brain; 01-26-2005 at 01:57 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    I found unions are kinda fun for this stuff. Not that its a great solution, but they work. Say something like:

    Code:
    union ByteSplit
    {
        16_t int16;
        8_t int8[2];
    };
    
    ByteSplit Value;
    Value.int16 = OriginalValue;
    //Value.int8[0] is 8 high bits
    //Value.int8[1] is low 8 bits
    I used that mainly for IP addresses:
    Code:
    union IP
    {
        int intIP;
        char cIP[4];
    };
    The API's want the int version, but most people can only work with the char versions.

    Don't think that works for signed values but never tried it.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Exile
    I found unions are kinda fun for this stuff. Not that its a great solution, but they work. Say something like:

    Code:
    union ByteSplit
    {
        16_t int16;
        8_t int8[2];
    };
    
    ByteSplit Value;
    Value.int16 = OriginalValue;
    //Value.int8[0] is 8 high bits
    //Value.int8[1] is low 8 bits

    Unless the system is little-endian, in which case Value.int8[0] holds the low bits and Value.int8[1] holds the high bits.

    Regards,

    Dave

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Quote Originally Posted by Dave Evans
    Unless the system is little-endian, in which case Value.int8[0] holds the low bits and Value.int8[1] holds the high bits.
    Does taht same problem exist for the solutions that use bit shifts? The shifts would work, but the results of the shift are switched.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Exile
    Does taht same problem exist for the solutions that use bit shifts? The shifts would work, but the results of the shift are switched.
    That was my point: Bit shifts (or arithmetic with '/' and '%' operators) allow you to extract the bytes whatever order you require. (Not endian-dependent.)

    The shift and arithmetic operators work on numbers in registers. Upper bits are upper bits; lower bits are lower bits.

    When the individual bytes get stored in memory, the byte order is dependent on the endianness of the machine. This shows up with different results for bid-endian systems and little-endian systems using the union.

    Regards,

    Dave
    Last edited by Dave Evans; 01-26-2005 at 03:51 PM.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    I didn't know that. I figured that once the stuff was in memory it was all in the same order. Guess I lucked out and all of the stuff I used was big-endian.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. winsock splitting msg
    By abraham2119 in forum Networking/Device Communication
    Replies: 10
    Last Post: 07-05-2009, 04:24 AM
  2. String splitting algorithm help
    By (TNT) in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2007, 11:28 AM
  3. Problem splitting program into different files
    By kzar in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:03 AM
  4. Splitting the Window
    By cweb255 in forum Windows Programming
    Replies: 4
    Last Post: 05-30-2005, 06:19 AM
  5. Splitting a dialog up into multiple classes
    By Just in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 11:11 PM