Results 1 to 5 of 5

Thread: EXCEL: How To: Convert Byte Array To Single [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Resolved 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

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    4.  
    5. Function ConvertHEXtoSingle(Bynary As String) As Single
    6.  
    7. Dim abBytes(0 To 3) As Byte
    8. Dim lByte As Long
    9.  
    10.     For lByte = UBound(abBytes) To LBound(abBytes) Step -1
    11.        
    12.         abBytes(lByte) = "&H" & Left(Bynary, 2)
    13.         Bynary = Right(Bynary, Len(Bynary) - 2)
    14.        
    15.     Next lByte
    16.    
    17.     CopyMemory ConvertHEXtoSingle, abBytes(0), 4
    18.  
    19. End Function
    20.  
    21.  
    22. Sub testit()
    23. Dim sngTestValue
    24.    
    25.     sngTestValue = ConvertHEXtoSingle("C1020000")
    26.    
    27.     Debug.Print sngTestValue
    28. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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
  •  



Click Here to Expand Forum to Full Width