0

I have an uchar array with this arrangement :

uchar a={LowByte_val1 HighByte_val1 LowByte_val2 HighByte_val2 ...};

how to i can convert this array to (without loop)

uint b= [val1 val2 ...];

where

val1 = LowByte_val1+(HighByte_val1<<8);

I use this solution.

typedef union {
           struct { 
                    uchar lsb; 
                    uchar msb; 
                  }byteModel;
         unsigned int data; 
   }XXX;

and

uchar a[24]={0,0,0,1,0,2,0,3, 1,0,1,1,1,2,1,3, 2,0,1,1,2,2,2,3}; 
XXX *input = new XXX[12];

and then :

memcpy(input,a,24);

but not worked :(.

JaberRouhi
  • 15
  • 8

1 Answers1

0

You could use:

char c[size];
int *i = (int*)c;

This would allow you to access the same data as both an int and a char.

Keep in mind though (as I'm sure you already are) that ints and chars are not the same size. The int array will be much "shorter".

EDIT: Someone has pointed out that this only works with little endian architecture. If you need a big endian solution, I will point you to this tutorial. If you don't know what either of those are, I point you to this question and the above tutorial.

  • is there any solution if the arrangement was : uchar a={HighByte_val1 LowByte_val1 HighByte_val2 LowByte_val2 ...}; ???? – JaberRouhi Jan 30 '19 at 20:03
  • @user2087181 Yay! I'm glad. –  Jan 30 '19 at 20:03
  • @OznOg Please explain. What do you mean by "little endian arch"? Can I read about it somewhere? –  Jan 30 '19 at 20:04
  • see https://stackoverflow.com/questions/12825632/when-endianess-does-matter-cast-operations – OznOg Jan 30 '19 at 20:07