Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert hex string to double 1

Status
Not open for further replies.

moonbase

Programmer
Nov 18, 2002
57
GB
Can anyone advise on how to convert string to double? I am reading an incoming hex string such as
40 B2 FC 6B 85 1E B8 52
This supposedly represents a numeric double data type. How can I convert it?

What is the value of the above no?
 
You can check your work with calc.exe ...the little calculator program that comes with windows.

First put it into scientific mode. Then switch it into Hex mode. Next type in your hex number. Finnally switch it back into Decimal mode.
 
You can't translate the bytes simply as Hex. For specification for standard Doubles see
There is another view at:

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
>If it is a string put &H in front of it

Sadly, that just won't work. VB only handles hex numbers of up to 8 characters, and it only handles whole numbers (i.e integers/longs rather than doubles)

Here's one potential method which avoids any nasty calculations or bit manipulation:
Code:
[blue]Option Explicit

Private Type vbString
    bytes(1 To 8) As Byte
End Type

Private Type vbDouble
    proxieDouble As Double
End Type

Private Sub Command1_Click()
    Dim myHexString As vbString
    Dim myDouble As vbDouble
    
    myHexString.bytes(1) = &H40
    myHexString.bytes(2) = &HB2
    myHexString.bytes(3) = &HFC
    myHexString.bytes(4) = &H6B
    myHexString.bytes(5) = &H85
    myHexString.bytes(6) = &H1E
    myHexString.bytes(7) = &HB8
    myHexString.bytes(8) = &H52
    
    LSet myDouble = myHexString
    
    MsgBox myDouble.proxieDouble
End Sub[/blue]


 
Thanks, worked perfectly.

To get the correct result I had to reverse the order, presumably to take account of little endian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top