Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Big -> Little Endian (All)

    How can I read a double from a file written in whichever endian I'm not in?

    For example, if the bytes in the file are &H 40 42 49 11 6A 75 97 14
    then reading them into a double gives 1.78388164496443 E-209 which is the wrong answer!
    I want to "flip" the bytes to &H 14 97 75 6A 11 49 42 40
    which would give 36.57084398977 which is the correct answer.
    TIA --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Big -> Little Endian (All)

    Read 'em in as Hex and convert to decimal?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Big -> Little Endian (All)

    That works fine for integer and long, but for single and double, you would have to know how floating point numbers are constructed (I'm not going there!). The only way that I see is to read it into a byte array, "flip" the bytes, write the flipped bytes out to a temporary file and read them back in as single or double. Why can't VB allow you to put two different types into the same location? Seems like variant allows all the bad techniques and dosn't allow what you really need.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Big -> Little Endian (All)

    API to the rescue:
    <pre>Option Explicit
    '
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (Destination As Any, Source As Any, ByVal Length As Long)
    '
    Sub dblEndianSwap(d As Double)
    Dim h1(8) As Byte, h2(8) As Byte
    Dim s As String, i As Integer
    CopyMemory h1(1), d, 8
    For i = 1 To 8
    h2(i) = h1(9 - i)
    Next i
    CopyMemory d, h2(1), 8
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Big -> Little Endian (All)

    Good ol' CopyMemory. Where would we be without 'er?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

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