none
Convert byte[] to long RRS feed

  • Question

  • All of the searching I have done does not explain what I need.  I am reading data from a buffer and trying to convert the byte[] to a long value that is then displayed as text.  I have it working for byte, short, int and float but cannot get the long to work.  I just used, for example, BitConverter.ToInt16(buffer, 3).ToString() for short and similar conversions for the others but cannot make the long work.  It produces a very large number when I calculate it.

    BitConverter.ToInt64(buffer, 3).ToString() is what I have and it does not output the right number.

    Tuesday, March 5, 2013 7:50 PM

Answers

  • BitConverter.ToInt64 is correct.  Based upon your code the buffer starts with a 4-byte value because you're starting at offset 3.  It will then read the next 8 bytes and convert them to a long.  Using Calculator take the next 8 bytes starting at offset 3 from your buffer, place them in Calculator in hex mode and then switch it back to Dec to confirm that the value you're reading matches what you expected.  Keep in mind that some architectures use little endian and others use big endian.  If you're reading and writing the buffer using .NET code then you should be fine.  If the data is coming from another machine (like Unix or something) then it could be an endian problem.  My gut is your offset or data is wrong.

    As an alternative approach if you intend to read the buffer only once then you can wrap it in a BinaryWriter and then just read the types using the strongly typed Read methods.

    Michael Taylor - 3/5/2013
    http://msmvps.com/blogs/p3net

    • Marked as answer by N00b C Tuesday, March 5, 2013 9:23 PM
    Tuesday, March 5, 2013 8:47 PM
    Moderator

All replies

  • BitConverter.ToInt64 is correct.  Based upon your code the buffer starts with a 4-byte value because you're starting at offset 3.  It will then read the next 8 bytes and convert them to a long.  Using Calculator take the next 8 bytes starting at offset 3 from your buffer, place them in Calculator in hex mode and then switch it back to Dec to confirm that the value you're reading matches what you expected.  Keep in mind that some architectures use little endian and others use big endian.  If you're reading and writing the buffer using .NET code then you should be fine.  If the data is coming from another machine (like Unix or something) then it could be an endian problem.  My gut is your offset or data is wrong.

    As an alternative approach if you intend to read the buffer only once then you can wrap it in a BinaryWriter and then just read the types using the strongly typed Read methods.

    Michael Taylor - 3/5/2013
    http://msmvps.com/blogs/p3net

    • Marked as answer by N00b C Tuesday, March 5, 2013 9:23 PM
    Tuesday, March 5, 2013 8:47 PM
    Moderator
  • The values in my buffer are:

    [3] = 170

    [4] = 185

    [5] = 101

    [6] = 0

    I am trying to get 6666666 to output.


    What it gives me is 182725095307690.

    • Edited by N00b C Tuesday, March 5, 2013 9:23 PM
    Tuesday, March 5, 2013 9:01 PM
  • The first 4 bytes {AA, B9, 65, 0} in your buffer expressed as an integer are 6666666.  If you want it as a long, you'll have to convert it to an integer and then cast it to a long.
    Tuesday, March 5, 2013 9:15 PM
  • You can see http://msdn.microsoft.com/en-us/library/bb384066.aspx ,http://stackoverflow.com/questions/2289427/java-and-c-sharp-byte-array-to-long-conversion-difference

    The output may differ depending on the endianess of your computer's architecture.

    This example initializes an array of bytes, reverses the array if the computer architecture is little-endian (that is, the least significant byte is stored first), and then calls theToInt32(Byte[], Int32) method to convert four bytes in the array to an int. The second argument to ToInt32(Byte[], Int32) specifies the start index of the array of bytes.

    Constant denoting big-endian byte order. In this order, the bytes of a multibyte value are ordered from most significant to least significant.

    Constant denoting little-endian byte order. In this order, the bytes of a multibyte value are ordered from least significant to most significant.


    • Proposed as answer by Alaa El Bosaty Tuesday, March 5, 2013 11:07 PM
    Tuesday, March 5, 2013 11:07 PM