Trusted, 2s-Complement Binary Conversion

Jul 13, 2011 at 5:48am
Hi. Does anyone have any trusted code for converting various 8/16/32/64-bit, 2s-complement, binary fields to native integers?

I am a relatively weak (actively learning) C++ programmer needing to convert 8/16/32/64-bit, 2s-complement, bit streams (not text representation) to C++ native numbers. The data stream I receive is highly variable. I will be cutting-out the 8, 16, 32, and 64-bit 2s-complement fields from the data stream (as defined by the instructions inside the stream itself), then converting each 2s-complement field to a native, C++ integer.

My intent is to create a cross-platform-supportive class that can act on each of the four bit lengths, as needed, and provide a single (large) result that I then process natively.

Thank you for any trusted code you suggest.
Last edited on Jul 13, 2011 at 2:39pm
Jul 13, 2011 at 7:39am
Twos complement is the most commonly used representation. Most of the time you'd be converting it to itself.
And if you do have a machine which uses something else, the algorithm depends on what that is.
Jul 13, 2011 at 3:02pm
Thanks, hamsterman. Once I get a copy of the data feed, I'll just try to cast the data to standard int/long/short integers for a quick sanity check. I may have to byte-swap, too.

Thanks.
Topic archived. No new replies allowed.