Results 1 to 5 of 5

Thread: Bytes to floating point [RESOLVED]

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Question Bytes to floating point [RESOLVED]

    I want to convert 4 bytes in row to a single precision number. These are read from a file by means of Get commands but it's in big endian order so I must reverse them.

    Specifically, I'd like to know if there is a similar function to ntohs and ntohl for single and double precision floating point numbers. Or is there any command to read a file backwards?
    Last edited by krtxmrtz; Nov 3rd, 2003 at 09:22 AM.

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    This method would seem fast:

    VB Code:
    1. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    2.  
    3. '---
    4.  
    5. Dim ByteAry(3) as byte
    6. Dim lngRet as Long
    7.  
    8. CopyMemory ByteAry(0),SourceAry(start),4
    9. byteAry = StrConv(StrReverse(StrConv(byteAry,vbUnicode)),vbFromUnicode)
    10. CopyMemory lngRet, ByteAry(0), 4

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Your code gives a few errors. How is SourceAry to be defined? What does the variable start mean? Why are you using lngRet if I need a single precision value?

    My problem is: I am reading bytes 3F80 0000 from a file which correspond to floating 1.000 in the big endian order. However, my Get statement interprets the bytes in the little endian order as floating 4.6E-41. How exactly am I to apply your code to my specific case?

  4. #4
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    VB Code:
    1. Dim ByteAry() As Byte
    2. Dim SourceAry() As Byte
    3. Dim Ret As Single
    4. ReDim ByteAry(3)
    5. ReDim SourceAry(3)
    6. 'Open file
    7. 'read your 4 bytes into sourceary
    8. 'close
    9. SourceAry = StrConv(Chr$(&H3F) & Chr$(&H80) & Chr$(0) & Chr$(0), vbFromUnicode)
    10.  
    11. CopyMemory ByteAry(0), SourceAry(0), 4
    12. ByteAry = StrConv(StrReverse(StrConv(ByteAry, vbUnicode)), vbFromUnicode)
    13. CopyMemory Ret, ByteAry(0), 4
    14. MsgBox Ret

    Ok, tested, and working. Has to use redim to be compatible with StrConv.
    Last edited by DiGiTaIErRoR; Nov 2nd, 2003 at 02:44 PM.

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    OK now it works. Actually I had to change the StrConv line to

    SourceAry = StrConv(Chr$(SourceAry(0)) & Chr$(SourceAry(1)) & Chr$(SourceAry(2)) & Chr$(SourceAry(3)), vbFromUnicode)

    of course.

    StrReverse(!sknahT)

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