[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!
Re: Reading Bytes (Little Endian/Big Endian)
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)
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)
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
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.
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,
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)
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.
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.
Re: Reading Bytes (Little Endian/Big Endian)
I don't think so. It's a bin file.
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.
Re: Reading Bytes (Little Endian/Big Endian)
Quote:
Originally Posted by
Mal1t1a
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.
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.
1 Attachment(s)
Re: Reading Bytes (Little Endian/Big Endian)
Here's the file. It's Unicode (or atleast I'm pretty sure it is).
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())
Re: Reading Bytes (Little Endian/Big Endian)
Quote:
Originally Posted by
MattP
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!
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("")