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.
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)
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)
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.
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.
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.
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.
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())
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!
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("")