Convert char to short

I need to copy data from 2 char (8 bits) to one short (16 bits). I tried two different ways, but I can't get it to work.

 void char2short(char* pchar, short* pshort) { memcpy(pshort , pchar + 1 , 1); memcpy(pshort + 1, pchar , 1); } 

And another:

 void char2short(char* pchar, short* pshort) { short aux; aux = ((*pchar & 0x00FF) << 8) | ((*(pchar+1) & 0xFF00) >> 8); *pshort = aux; } 
+6
source share
4 answers
 #include <stdio.h> void char2short(unsigned char* pchar, unsigned short* pshort) { *pshort = (pchar[0] << 8) | pchar[1]; } int main() { unsigned char test[2]; unsigned short result = 0; test[0] = 0xAB; test[1] = 0xCD; char2short(test, &result); printf("%#X\n",result); return 0; } 

it will do the job.

+9
source

Assuming pchar is an array containing your 2 characters, how about:

 *pshort = (uint16_t)(((unsigned int)pchar[0]) | (((unsigned int)pchar[1])<<8)); 

PS This work is for a little enthusiasm.

+6
source

Others have not explained why your code does not work, so I will quickly hit it:

 memcpy(pshort , pchar + 1 , 1); memcpy(pshort + 1, pchar , 1); 

Adding TYPE * p to the pointer moves the pointer using sizeof( TYPE ) increments (so it points to the next element, remember that this is defined only inside the array). Therefore, if pchar + 1 correct, pshort + 1 not (since it refers to the next short ).

 aux = ((*pchar & 0x00FF) << 8) | ((*(pchar+1) & 0xFF00) >> 8); 

Errr .... the right side is broken more than one. Firstly, *(pchar+1) is a char , and & 0xFF00 on a char will always give 0 (because a char is only 8 bits, at least on modern machines ...). And then you shift these 8 bits to the right ...?

And just in case, when you did not know about it, if you did not use 0x00FF on the left side (advancing *pchar in the width of the right operand), but ( char size) 0xFF, the result of this operation will still be of type char , and shifting this 8 bits to the left doesn't make much sense either (since the type doesn't expand magically).


Another way to do this hasn't mentioned union yet:

  #include <stdio.h> struct chars_t { // could also go for char[2] here, // whichever makes more sense semantically... char first; char second; }; union combo_t { // elements of a union share the memory, ie // reside at the same address, not consecutive ones short shrt; struct chars_t chrs; }; int main() { union combo_t x; x.chrs.first = 0x01; x.chrs.second = 0x02; printf( "%x", x.shrt ); return 0; } 

If you use this in a wider context, beware of debugging the structure.

+5
source

When performing bitwise operations, use a reliable code with real fixed-size integers with a known signature. This will prevent you from writing errors related to implicit type conversions, causing an unintended signature. The char type is especially dangerous, since it has a signature defined by the application. It should never be used to store numbers.

 #include <stdint.h> void char2short(const uint8_t* pchar, uint16_t* pshort) { *pshort = ((uint16_t)pchar[0] << 8) | (uint16_t)pchar[1]; } 
+2
source

Source: https://habr.com/ru/post/975129/


All Articles