I got the data for uint16_t
and it like 0x2c06
.
For the 0x2c06
, 2c
is the first data and the 06
is the second data.
How do I parse the 0x2c06
and turn the 2c
and the 06
to int?
Thanks in advance.
I got the data for uint16_t
and it like 0x2c06
.
For the 0x2c06
, 2c
is the first data and the 06
is the second data.
How do I parse the 0x2c06
and turn the 2c
and the 06
to int?
Thanks in advance.
You can use a char*
and some pointer arithmetic to get this done.
Pseudo-code:
uint16_t var = 0x2c06;
unsigned char * temp = &var;
int first = *temp; //or second, see note
int second = *(temp + 1); // or first, see note
Note: You need to take care of endianness
Build a set of case operators cast the uint and just equate it, eg
switch(sizeof unsign)
{
case sizeof (unsigned short int):
int result = (unsigned short int)unsign;
break;
default:
fprintf (stderr, "recast of unit_16 failed.\n");
}
Sorry if there's bugs in this, it's just typed into the browser.