|
-
May 9th, 2006, 08:19 AM
#1
Thread Starter
Frenzied Member
EXCEL: How To: Convert Byte Array To Single [RESOLVED]
Esteemed Forum Participants and Lurkers:
===============================
Excel 2003 VBA
How do I convert an Array of Bytes into a Single?
I know that the 4-Byte IEE 754 hex code in Single format for -8.125 = C1020000
I Dim a byte array:
Code:
Dim aByte(3) As Byte
aByte(3) = &HC1
aByte(2) = &H2
aByte(1) = 0
aByte(0) = 0
OK ... the order is probably reversed ... but how do I get this into a type Single variable "aSing" so that I can "MsgBox aSing" and display the correct decimal value? Of course, this is just asking how to implement C's "AddressOf" pointer in VBA.
Last edited by Webtest; May 9th, 2006 at 09:35 AM.
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
May 9th, 2006, 09:15 AM
#2
Re: EXCEL: How To: Convert Byte Array To Single ???
Art
Long time, no hear. Guess that new role is keeping you busy
Is this what you are looking to achieve?
VB Code:
Option Explicit
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Function ConvertHEXtoSingle(Bynary As String) As Single
Dim abBytes(0 To 3) As Byte
Dim lByte As Long
For lByte = UBound(abBytes) To LBound(abBytes) Step -1
abBytes(lByte) = "&H" & Left(Bynary, 2)
Bynary = Right(Bynary, Len(Bynary) - 2)
Next lByte
CopyMemory ConvertHEXtoSingle, abBytes(0), 4
End Function
Sub testit()
Dim sngTestValue
sngTestValue = ConvertHEXtoSingle("C1020000")
Debug.Print sngTestValue
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
May 9th, 2006, 09:35 AM
#3
Thread Starter
Frenzied Member
Re: EXCEL: How To: Convert Byte Array To Single ???
Geez Declan ... you wouldn't believe how busy!
I should have known that it would take an API call! Thanks for the help ... it works just fine. I was trying to help Peter2047. I have thought about address pointers in the past, but have never had sufficient motivation to dig deeper into it, but with Peter's problem, he needs a cleaner solution than rewriting his "C" file into a "VBA" file so that he can actually read the Float data. The file part I had done recently when I was looking at TTF file tables for a Bar Code font I was developing. Since I already had the routines and it looked like they might help Peter I spent a little time on it.
Thanks again ... ALL of you guys are great! Someday I'll be up there with you giants! but right now, I've got work to do!
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
May 9th, 2006, 09:36 AM
#4
Re: EXCEL: How To: Convert Byte Array To Single [RESOLVED]
Just glad to help. Catch you later.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
May 9th, 2006, 10:35 AM
#5
Thread Starter
Frenzied Member
Re: EXCEL: How To: Convert Byte Array To Single [RESOLVED]
Here is the finished product, thanks to Declan ... maybe it needs to go in the "Sticky" code bank?
Code:
Option Explicit
'API CALL PROTOTYPE
' Copy a block of memory from one Variable location to another
' "Casts" Destination Type to Source Type using 'back door' approach
'
Private Declare Sub CopyMemory Lib "KERNEL32" _
Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
'Function to Cast 32 bit IEE 754 BYTE ARRAY to TYPE "SINGLE"
' Calls "CopyMemory" API with pointer to Byte Array and Byte Count = 4
' Assumes aByte(3) is MOST SIGNIFICANT Byte, aByte(0) is LEAST SIGNIFICANT Byte
' We pass the Reference address of the Byte Array
' The Return is the byte code cast to Type Single
'
Function ByteArrayToSingle(ByRef aByte() As Byte) As Single
CopyMemory ByteArrayToSingle, aByte(0), 4
End Function
Sub TEST1()
Dim aSing As Single 'The variable to be overwritten with the Byte Array
Dim aByte(3) As Byte 'Value to write = -8.125736 (code = hex:C1020304)
aByte(3) = &HC1 'Sign bit and MS 7 bits of exponent
aByte(2) = &H2 'LSb of exponent and MS 7 bits of Significand
aByte(1) = &H3
aByte(0) = &H4 'LS 8 bits of Significand
aSing = ByteArrayToSingle(aByte)
MsgBox aSing
End Sub
All this for the C "AddressOf" Pointer!!!
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
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
|