none
[HELP] Problem reading bytes RRS feed

  • Question

  • Hi all, im trying to read 4 bytes but im getting the bytes in reverse. Example:

    In offset &H1000 i have 4D5A9A7B im getting 7B9A5A4D

    My code:

            Dim stream = File.OpenRead(fileDialogBox.FileName)
            Dim reader As New BinaryReader(stream)
            stream.Position = &H1000
            Dim bytes As Integer = reader.ReadInt32
            TextBox2.Text = bytes.ToString("X")


    • Edited by extream87 Thursday, June 11, 2015 2:11 AM
    Thursday, June 11, 2015 2:11 AM

Answers

  • Some operating systems (such as Windows) write the bytes of an integer with the lowest order first (this is called "little-endian" format). That means that if you use ReadInt32 to read 4 bytes, the first byte will become the low order of the integer and the fourth will be the high order (see the remarks section of the ReadInt32 documentation). Then when you use ToString("X") to convert the integer to a hex string, the high order byte (which was originally the fourth byte) becomes the first two hex digits in the string. If you want to read an array of bytes and keep them in the original order, use the ReadBytes method of BinaryReader.


    • Edited by Blackwood Thursday, June 11, 2015 2:41 AM
    • Proposed as answer by Frank L. Smith Thursday, June 11, 2015 3:05 AM
    • Marked as answer by extream87 Friday, June 12, 2015 12:56 AM
    Thursday, June 11, 2015 2:33 AM