Results 1 to 18 of 18

Thread: [RESOLVED] Reading Bytes (Little Endian/Big Endian)

  1. #1

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Resolved [RESOLVED] Reading Bytes (Little Endian/Big Endian)

    Hi there, I've stumbled upon another problem. I need to be able to read bytes in Little Endian and in Big Endian as well. Is this possible to do in VB? I know that VB reads in Little Endian by default, but I also need to read in Big Endian.

    I have tried this:
    Code:
    Dim i1 as Long
    Dim fs As New IO.FileStream(filename, IO.FileMode.Open)
    Dim br As New IO.BinaryReader(fs, System.Text.Encoding.BigEndianUnicode)
    Dim lBytes As Long = fs.Length
    fs.Position = 0
    i1 = br.ReadInt32()
    fs.close
    br.close
    fs.dispose
    Msgbox(i1)
    This return the value "1616183296" when it is supposed to return "21856". I've opened the file up in Hex Workshop, and looking at it in Little Endian, I do get the value of "1616183296" and in Big Endian, I get the value of "21856". So Obviously I'm not doing it right.

    Can anyone help with this? Thanks!

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Reading Bytes (Little Endian/Big Endian)

    I have now read it, and I've applied to what I have just learned to my program, and it gives the exact same results. Here's my revised code:

    Code:
    dim bigEndianBOM as new System.Text.UnicodeEncoding(True,True)
    Dim i1 as Long
    Dim fs As New IO.FileStream(filename, IO.FileMode.Open)
    Dim br As New IO.BinaryReader(fs, bigEndianBOM)
    Dim lBytes As Long = fs.Length
    fs.Position = 0
    i1 = br.ReadInt32()
    fs.close
    br.close
    fs.dispose
    Msgbox(i1)

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    I didn't test this but take a look at it? It should give us a clue about the failure.
    Code:
            Dim bigEndianBOM As New System.Text.UnicodeEncoding(True, True)
            Dim b(4) As Byte
            Dim fs As New IO.FileStream(filename, IO.FileMode.Open)
            Dim br As New IO.BinaryReader(fs, bigEndianBOM)
            Dim lBytes As Long = fs.Length
            fs.Position = 0
    
            b = br.ReadBytes(4)
            Stop 'what does b look like?
            fs.Close()
            br.Close()
            fs.Dispose()
            MsgBox(i1)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Reading Bytes (Little Endian/Big Endian)

    okay, I've taken a look at them, and They are both exactly the same.
    The values they contain:
    Code:
    b(0) = 0
    b(1) = 0
    b(2) = 85
    b(3) = 96

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    Try changing the b declaration to 16, and read 16 bytes. I am trying to determine what the order is.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Reading Bytes (Little Endian/Big Endian)

    This is what is returned (I put commas to seperate the bytes).

    litteEndian:
    0,0,85,96,64,64,64,64,64,74,56,16,64,7,61,73,

    bigEndian:
    0,0,85,96,64,64,64,64,64,74,56,16,64,7,61,73,

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    That should tell you something I think. What does true, true mean here

    dim bigEndianBOM as new System.Text.UnicodeEncoding(True,True)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Reading Bytes (Little Endian/Big Endian)

    The first True argument is true to have Big Endian Encoding, or false to have Little Endian Encoding. The second True, Is wether or not to use BOM (Byte Order Mark).

    Could this be my computer? Because I really do not see why its not working.

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    Is the file UniCode? That means several bytes per character? I ran a test against a word document and the encoding changed the results. But I know that word uses unicode.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Reading Bytes (Little Endian/Big Endian)

    I don't think so. It's a bin file.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    Here is a part test text file I have
    3456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012 3456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012 3456789012345678901234567890123456789012345678901234567890123456789012345678901234567890E

    Here part of it in unicode
    ÐÏࡱá >  þÿ   $  &  þÿÿÿ # ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ‹ ÿÿ ÿÿ ÿÿ ¤ ê 4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123 4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123 4567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123 45678901234567890E
    5678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234 56789012345678901234567890123456789012345678901234567890123456789012345678901234567890123    2   Microsoft Office Word @ @ žâ,õÛÊ@ žâ,õÛÊ   *  Þ  Microsoft Office Word Document
    MSWordDoc  Word.Document.8 ô9²q

    NotePad has a feature that allows you to open files in different formats, one of them being BigEndian.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    Quote Originally Posted by Mal1t1a View Post
    I don't think so. It's a bin file.
    So? What do you think is happening when you apply a unicode encoder to a file not encoded with unicode?

    Code:
    Using the big-endian and little-endian methods, the number 0x12345678 would be stored as shown in the following table.
    
    
    Byte order  Byte 0  Byte 1  Byte 2  Byte 3  
    Big-endian
                 0x12    0x34     0x56    0x78
     
    Little-endian
                 0x78    0x56     0x34    0x12
    If using integers, read four bytes and "OR" them into an integer( = 0) in the appropriate place.
    Last edited by dbasnett; Apr 14th, 2010 at 12:53 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    I have to go to the doctors. If someone else doesn't help I will when I get home.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Reading Bytes (Little Endian/Big Endian)

    Here's the file. It's Unicode (or atleast I'm pretty sure it is).
    Attached Files Attached Files

  16. #16
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Reading Bytes (Little Endian/Big Endian)

    This returns the correct values for BigEndian and LittleEndian.

    Code:
            Dim beBOM As New System.Text.UnicodeEncoding(True, True)
            Dim littleEndian As Long
            Dim bigEndian As Long
            Using fs As New IO.FileStream("C:\Temp\theFile.bin", IO.FileMode.Open),
            br As New IO.BinaryReader(fs, beBOM)
                fs.Position = 0
                Dim startIdx As Integer = 0
                Dim b() As Byte = br.ReadBytes(4)
                littleEndian = BitConverter.ToInt32(b, startIdx)
                bigEndian = BitConverter.ToInt32(b.Reverse.ToArray, startIdx)
            End Using
    
            MessageBox.Show(littleEndian.ToString())
            MessageBox.Show(bigEndian.ToString())

  17. #17

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Reading Bytes (Little Endian/Big Endian)

    Quote Originally Posted by MattP View Post
    This returns the correct values for BigEndian and LittleEndian.

    Code:
            Dim beBOM As New System.Text.UnicodeEncoding(True, True)
            Dim littleEndian As Long
            Dim bigEndian As Long
            Using fs As New IO.FileStream("C:\Temp\theFile.bin", IO.FileMode.Open),
            br As New IO.BinaryReader(fs, beBOM)
                fs.Position = 0
                Dim startIdx As Integer = 0
                Dim b() As Byte = br.ReadBytes(4)
                littleEndian = BitConverter.ToInt32(b, startIdx)
                bigEndian = BitConverter.ToInt32(b.Reverse.ToArray, startIdx)
            End Using
    
            MessageBox.Show(littleEndian.ToString())
            MessageBox.Show(bigEndian.ToString())

    Thank-you very much Mattp! That's exactly what I was looking for. You were of great help! Also, thank-you dbasnet, your help was appreciated!

  18. #18
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Reading Bytes (Little Endian/Big Endian)

    I didn't know that you could use a unicode encoder on a non-unicode file. Learn soemthing new every day.

    Code:
           Dim mask As Integer = &HFF 'mask
            Dim testval As Integer = 1234567899 '49 96 02 DB
    
            Dim b() As Byte = New Byte() {0, 0, 0, 0}
            Dim shft As Integer = 0
    
            'show little endian
            Debug.WriteLine("")
            For x As Integer = 0 To 3
                shft = x * 8
                b(x) = CByte((testval >> shft) And mask)
                Debug.Write(Convert.ToString(b(x), 16).ToUpper.PadLeft(2, "0"c) & " ")
            Next
            'debug output of little endian
            'DB 02 96 49 
            Debug.WriteLine("")
    
            'show big endian
            Array.Reverse(b)
            Debug.WriteLine("")
            For x As Integer = 0 To 3
                shft = x * 8
                Debug.Write(Convert.ToString(b(x), 16).ToUpper.PadLeft(2, "0"c) & " ")
            Next
            'debug output of big endian
            '49 96 02 DB
            Debug.WriteLine("")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width