-2

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.

Martin
  • 2,813
  • 11
  • 43
  • 66

2 Answers2

1

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

Kyrol
  • 3,475
  • 7
  • 34
  • 46
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
-2

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.

phil
  • 561
  • 3
  • 10