combining chars
-
hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit
-
hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit
lets assume that u have a buffer of type (char *) called charbuffer you can convert it like this (I havent tested it but it should work): short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer. regards Mohammad
And ever has it been that love knows not its own depth until the hour of separation
-
hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit
-
hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit
int intvalue = ((buffer[5] << 8) | buffer[6]) & 0x0000FFFF; Edit: Forgot a parenthesis :)
-
lets assume that u have a buffer of type (char *) called charbuffer you can convert it like this (I havent tested it but it should work): short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer. regards Mohammad
And ever has it been that love knows not its own depth until the hour of separation
Mohammad A Gdeisat wrote:
short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer.
FYI that won't work on an Intel x86 machine in his case because the most significant byte is FIRST in the char array. Mark
-
Mohammad A Gdeisat wrote:
short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer.
FYI that won't work on an Intel x86 machine in his case because the most significant byte is FIRST in the char array. Mark
then u can use masking and shifting to swap between hi-order and low-order bytes. Regards, Mohammad
And ever has it been that love knows not its own depth until the hour of separation