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!

5
  • 1
    .Net doesn't have a UInt128 type. What do you want to do with the integer? Commented Jun 9, 2010 at 15:08
  • 3
    Are you talking about this UInt128 data type: msdn.microsoft.com/en-us/library/cc230384%28PROT.10%29.aspx or the BigInteger (msdn.microsoft.com/en-us/library/…) struct introduced with .NET 4.0? Commented Jun 9, 2010 at 15:09
  • 1
    Reading the 16 bytes from the stream and storing them into an array won't be a problem. Converting them to an integer will be as there's no such integral type in .NET that is able to represent 128bit integers. So you will have to roll your own or use BigInteger (msdn.microsoft.com/en-us/library/dd268207(v=VS.100).aspx) from .NET 4.0. Commented Jun 9, 2010 at 15:10
  • @0xA3: +1 if I could for BigInteger :) Commented Jun 9, 2010 at 15:49
  • Hmm, how did this "16-byte integer" get into the stream in the first place? There is no common language that I know that supports such a type natively. Commented Jun 9, 2010 at 15:57

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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 (see clarification in comments for detailed explanation) - 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??

4 Comments

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.
Agreed with @0xA3. The OP asks for storing 128-bit unsigned integers.
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
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.
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.