|
-
Dec 14th, 2011, 09:57 AM
#1
Thread Starter
Frenzied Member
Reading Bytes From File Endian
Hello
Need a little help, never done similar to what i'm trying to do.
So i have one file, that has one header that tells me if the bytes are in little or big endian format.
The content of the file after the file is something like this, one byte that indicates a new "section", then several bytes until the other "section". This bytes each one represent the number of the bytes that i need to read in the second section.
Sample file
Code:
<Header EndianInfo>
<StartSection>01 03 02
<StartSection>AA BB BB BB CC CC
and so on...
So what i need to read it's AA get the decimal value, the BB BB BB and get the decimal value and CC CC and get the decimal value.
I know several ways to do this, but I don't know what's the best way to accomplish this.
For example i can do the job with this code:
VB.NET Code:
Dim BR As New IO.BinaryReader(IO.File.OpenRead(_fitActivitieFile))
BR.BaseStream.Seek(local, IO.SeekOrigin.Begin) 'Just to put in first byte that i need to read
Dim currentBytes() As Byte = BR.ReadBytes(numBytes) 'Read the number of bytes that i need
Debug.WriteLine(Val("&H" & BitConverter.ToString(currentBytes).Replace("-", ""))) 'BigEndian
Array.Reverse(currentBytes) 'Just activate when the format it's little endian
Debug.WriteLine(Val("&H" & BitConverter.ToString(currentBytes).Replace("-", ""))) 'LIttleEndian
But i don't like this version, with replaces and concatenating strings...
Can anyone show me another way to do this?
Thanks
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Dec 14th, 2011, 10:30 AM
#2
Re: Reading Bytes From File Endian
Here's an example I wrote a couple of years ago working with Big Endian and Little Endian files: http://www.vbforums.com/showthread.p...00#post3777900.
Hopefully you'll be able to adapt it to your code so you're happy with the solution.
-
Dec 14th, 2011, 10:43 AM
#3
Thread Starter
Frenzied Member
Re: Reading Bytes From File Endian
Thanks, but for what i see it's more or less the same thing that i have, the only difference that i see it's that you use the BitConverter.ToInt, but i can't use it because i have some lines with 3 bytes, if i use the Int16 i loose the value, if i use the in32 the compiler complains about something related to the destination array.
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Dec 14th, 2011, 10:56 AM
#4
Re: Reading Bytes From File Endian
You're right ToInt32 is looking for 4 bytes.
At the bottom of the page: http://msdn.microsoft.com/en-us/libr...converter.aspx The question "Can we get an overload that generates a hex string without the dashes?" is asked. A couple of workarounds are listed right after.
-
Dec 14th, 2011, 02:06 PM
#5
Re: Reading Bytes From File Endian
Just Create your own Converter, on any Integral data type you can call .ToString("X2") And It will pad the value so that it is 2 characters, for example if you have the integer value of 16 and you convert it with above tostring it becomes 0F, You can change the 2 with whatever number you need it padded to, or not spesify the number and it will use the default for the datatype.
EDIT: For example
Code:
Function BytesToString(ByVal Buffer() As Byte) As String
Dim st As New System.Text.StringBuilder(Buffer.Length * 2)
For i = 0 To Buffer.Length - 1
st.Append(Buffer(i).ToString("X"))
If i <> Buffer.Length - 1 Then st.Append(" "c)
Next
Return st.ToString.Trim
End Function
EDIT:2 If you need to reverse the it you can use this
Code:
Function BytesToStringex(ByVal Buffer() As Byte) As String
Dim st As New System.Text.StringBuilder(Buffer.Length * 2)
For i = Buffer.Length - 1 To 0 Step -1
st.Append(Buffer(i).ToString("X"))
If i <> 0 Then st.Append(" "c)
Next
Return st.ToString
End Function
Last edited by BlindSniper; Dec 14th, 2011 at 02:25 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|