|
-
Nov 1st, 2003, 06:17 PM
#1
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.
-
Nov 1st, 2003, 07:17 PM
#2
So Unbanned
This method would seem fast:
VB Code:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'---
Dim ByteAry(3) as byte
Dim lngRet as Long
CopyMemory ByteAry(0),SourceAry(start),4
byteAry = StrConv(StrReverse(StrConv(byteAry,vbUnicode)),vbFromUnicode)
CopyMemory lngRet, ByteAry(0), 4
-
Nov 2nd, 2003, 01:25 PM
#3
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?
-
Nov 2nd, 2003, 02:39 PM
#4
So Unbanned
VB Code:
Dim ByteAry() As Byte
Dim SourceAry() As Byte
Dim Ret As Single
ReDim ByteAry(3)
ReDim SourceAry(3)
'Open file
'read your 4 bytes into sourceary
'close
SourceAry = StrConv(Chr$(&H3F) & Chr$(&H80) & Chr$(0) & Chr$(0), vbFromUnicode)
CopyMemory ByteAry(0), SourceAry(0), 4
ByteAry = StrConv(StrReverse(StrConv(ByteAry, vbUnicode)), vbFromUnicode)
CopyMemory Ret, ByteAry(0), 4
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.
-
Nov 3rd, 2003, 09:21 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|