Start New Discussion within our Software Development Community

Hi there,

I have an array of unsigned chars that hold hex values, I’m interested in values [5] and [6] of the array.

Say [5] = 0x5b = 0101 1011
and [6] = 0x7b = 0111 1011

I'm trying to get all of [5] and the first half of [6], so 1011011 1111. So far I’ve come up with this;

short int holder = 0;
memcpy(holder,&char_array[5], 2 );
short int alt = holder >> 4;
return 0;

I've tried creating a 14 bit data type to hold the bits, copying the 2 bytes in then shifting right 4 but this does not work. I appreciate this is probably not the right way to do this so any help would be greatly appreciated.

Cheers

Alex

You say that you want all of [5] and the first half of [6] but your example does not show that. If you have 0101 1011 and 0111 1011 and you want the first with the 4 least significant bytes of the second you would get 0101 1011 1011. Or, if you want the most significant bytes of the second you get 0101 1011 0111. Neither of those is what you show.

You can use the following as a way to experiment with what you want:

int main () {                                                                   

    unsigned char a = 0x5b, b = 0x7b;                                           
    int bmap1 = a, bmap2 = a;                                                   
    bmap1 = (bmap1 << 4) | (b & 0x0f);                                          
    bmap2 = (bmap2 << 4) | ((b & 0xf0) >> 4);                                   
    printf ("a     : 0x%X\n", a);                                               
    printf ("b     : 0x%X\n", b);                                               
    printf ("bmap1 : 0x%04X\n", bmap1);                                         
    printf ("bmap2 : 0x%04X\n", bmap2);                                         

    return 0;                                                                   
}
This question has already been answered. Start a new discussion instead.