Thread: typecast array of bytes to larger data types

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    typecast array of bytes to larger data types

    Hello all, I'm working with a serial protocol where I get an array of bytes. In the array there is are different data types serialized. Is there a way of type casting the array to extract the data directly like perhaps: (UINT16)*(&array[2]) ? I want to treat array[2] and array[3] as a 16bit data type but that code didn't do the job. I worked around it by using memcpy to copy those 2 memory locations into a 16bit data type but I bet one of you geniuses knows what's the proper way to typecast the array location directly.

    Thanks for the help.

    David P

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You want something like this.
    Code:
    #include <stdio.h>
    #include <inttypes.h>
    
    int main(void) {
        unsigned char array[10] = { 0 };
        uint16_t i = *(uint16_t*)&array[2];
        printf("%d\n", i);
        return 0;
    }
    But beware of issues of endianness.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You could create a pointer to a UINT16 and set the pointer to the address of array[2], thusly:

    Code:
    UINT16 *derp, value;
    
    derp = (UINT16 *) &array[2];
    value = *derp;
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by codegrush View Post
    Hello all, I'm working with a serial protocol where I get an array of bytes. In the array there is are different data types serialized. Is there a way of type casting the array to extract the data directly like perhaps: (UINT16)*(&array[2]) ? I want to treat array[2] and array[3] as a 16bit data type but that code didn't do the job. I worked around it by using memcpy to copy those 2 memory locations into a 16bit data type but I bet one of you geniuses knows what's the proper way to typecast the array location directly.
    Yes I know the way of casting it and accesssing directly, but that is the worst way to do it. If you typecast a misaligned address then either your program will crash, or it will take at least twice as long as a normal read, depending on the architecture.

    Using memcpy on the other hand wont crash, but will give the wrong result if the data is not in the same endian format as your machine.

    The correct way to do it is by reconstructing the value using bitshifts. You can get away with doing it other ways in several cases, but it's not kosher.
    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"

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    As iMalc said, you actually need to do this by shifting a byte at a time into the larger variable in order to avoid alignment problems, and also to adjust the endianness. So something like this:
    Code:
    #include <inttypes.h> // for uint16_t, etc.
    
    unsigned char a[100];
    uint16_t u;
    int i = 0;
    
    // TODO: read data into a
    
    u = (uint16_t)a[i++] << 8;
    u |= a[i++];
    
    // or for the other endianness
    u = a[i++];
    u |= (uint16_t)a[i++] << 8;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 01-04-2012, 06:32 AM
  2. Joining 2 data (bytes) array
    By susinaga in forum C Programming
    Replies: 3
    Last Post: 08-04-2010, 10:04 AM
  3. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  4. Types, Integral Types, Bytes?!?!?!
    By Kaidao in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2006, 08:15 AM
  5. Possible to pack smaller data types into larger ones?
    By Heraclitus in forum C Programming
    Replies: 3
    Last Post: 02-09-2003, 03:22 PM

Tags for this Thread