Results 1 to 5 of 5

Thread: Reading Bytes From File Endian

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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:
    1. Dim BR As New IO.BinaryReader(IO.File.OpenRead(_fitActivitieFile))
    2. BR.BaseStream.Seek(local, IO.SeekOrigin.Begin) 'Just to put in first byte that i need to read
    3. Dim currentBytes() As Byte = BR.ReadBytes(numBytes) 'Read the number of bytes that i need
    4.  
    5. Debug.WriteLine(Val("&H" & BitConverter.ToString(currentBytes).Replace("-", ""))) 'BigEndian
    6. Array.Reverse(currentBytes) 'Just activate when the format it's little endian
    7. 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

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

    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.

  3. #3

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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

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

    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.

  5. #5
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    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.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

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