Sign in to follow this  

two's complement conversion help

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My data comes as a stream of bytes out of a wavefile: byte0 = low order sample0 L byte1 = high order sample0 L byte2 = low order sample0 R byte3 = high order sample0 R byte4 = low order sample1 L byte5 = high order sample1 L byte6 = low order sample1 R byte7 = high order sample1 R etc... byte1 and byte0 make up one 16 bit signed word. I'd like to convert these bytes into an integer in the range -32768 -> 32767. I'd also like to convert them back to put back into the stream... Can anyone help me figure out how to do this in C? Thanks!

Share this post


Link to post
Share on other sites

int16bits = highbyte<<8 | lowbyte;
lowbyte = int16bits & 0xFF;
highbyte = int16bits>>8 & 0xFF;

To get it in the range -32768..32767 simply use a signed short

Share this post


Link to post
Share on other sites

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this