views:

183

answers:

3

I need to parse a binary stream in .NET to convert a 16 byte unsigned integer. I would like to use the BinaryReader.ReadUIntXX() functions but there isn't a BinaryReader.ReadUInt128() function available. I assume I will have to roll my own function using the ReadByte function and build an array but I don't know if this is the most efficient method? Thanks!

+1  A: 

I would love to take credit for this, but one quick search of the net, and viola:

http://msdn.microsoft.com/en-us/library/bb384066.aspx

Here is the code sample (which is on the same page)

byte[] bytes = { 0, 0, 0, 25 };

// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25

The only thing that most developers do not know is the difference between big-endian, and little-endian. Well like most things in life the human race simply can't agree on very simple things (left and right hand cars is a good example as well). When the bits (remember 1 and 0's and binary math), are laid out the order of the bits will determine the value of the field. One byte is eigth bits.. then there is signed and unsigned.. but lets stick to the order. The number 1 (one) can be represented in one of two ways , 10000000 or 00000001 - as the comment in the code suggests, the big-endian is the one with the one in front, litte-endian is the one with zero. (see http: // en.wikipedia.org/wiki/Endianness -sorry new user and they wont' let me hyperlink more than once....) Why can't we all just agree???

I learned this lesson many years ago when dealing with embedded systems....remember linking? :) Am I showing my age??

codputer
This is most likely not going to work as the question asks for 128-bit values. But we can't know for sure as there are not enough details given.
0xA3
Agreed with @0xA3. The OP asks for storing 128-bit unsigned integers.
Darin Dimitrov
Actually you are wrong about Endianness Representing 1 in a 16 bit system in Big Endian is 00000000 00000001 where in little endian is 00000001 00000000 not 10000000 00000000
Scott Chamberlain
I can confirm everyone's comments are correct; this won't work and the endian descriptions are incorrect. scott chamberlain provided corrected clarification. thanks again for the help.
Brent
A: 

a guid is exactly 16 bytes in size.

Guid guid = new Guid(byteArray);

But you cannot do maths with a Guid. If you need to, you can search for some implementations of a BigInteger for .net on the internet. You can then convert your bytearray into a BigInteger.

codymanix
A: 

I think the comments from 0xA3, SLaks, and Darin Dimitrov answered the question but to put it all together. BinaryReader.ReadUInt128() is not supported in the binary reader class in .NET and the only solution I could find was to create my own function. As 0xA3 mentioned, there is a BigInt data type in .NET 4.0. I am in the process of creating my own function based upon everyone's comments. Thanks!

Brent