Monday, May 24, 2010

ALL C programmers I need your help.?

I'm decoding a file using parity checking and checksum detections. Theres a 16bit value on the end of the date grouping. I've read it into an array using fread.





fread(%26amp;checksum_value[0], 2, 1, fp);


(this stores into array checksum_value, 2 bytes of info, 1 btye long from where the file pointer is pointing.)





My question is this, how do I get this to be an integer value. I've tried to use atoi, and strtol, with no luck... ANY ideas would be great.





Thanks in advance,

ALL C programmers I need your help.?
If checksum_value is of type unsigned char, you can convert it by shifting and ORing:





/* watch out for endianness */


int value = (checksum_value[1] %26lt;%26lt; 8) | (checksum_value[0]);





Alternatively, you could declare a 16-bit short int and read in the data w/o having to convert it manually:





short int value;


fread(%26amp;value, sizeof(value), 1, fp);

imperial

No comments:

Post a Comment