-
March 11th, 2011, 11:54 AM
#1
2's compliment conversion
Hi
I have 3 bytes of data that I am told represent a 24 bit integer. I am also told that the the data is in 2's compliment.
How do i covert the bytes to an integer value
Thanks for your time
-
March 11th, 2011, 12:51 PM
#2
Re: 2's compliment conversion
-
March 12th, 2011, 08:39 AM
#3
Re: 2's compliment conversion
Code:
unsigned char data[3];
int result = ((data[0] << 24) + (data[1] << 16) + (data[2] << 8)) >> 8;
Assuming the data is big-endian and that int is 32 bits on your platform. The last shift is to sign extend the result.
-
March 13th, 2011, 05:44 AM
#4
Re: 2's compliment conversion
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|