-
Jun 22nd, 2021, 05:41 AM
#1
Thread Starter
Member
Decimal to Hexadecimal in Half instead of Double
How can one modify the below code to output 16-bit Half instead of 64-bit Double?
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long _
)
Private Function DoubleToHEX(dnumber As Double) As String
Dim HexBytes(0 To 7) As Byte
Dim i As Integer
Dim x As String
CopyMemory HexBytes(0), dnumber, 8
x = ""
For i = 0 To 7
x = HEX00(HexBytes(i)) & x
Next i
DoubleToHEX = x
End Function
Private Function HEX00(bNumber As Byte) As String
Dim x As String
x = "0" & Hex(bNumber)
HEX00 = Right(x, 2)
End Function
Private Function HEXtoDouble(hNumber As String) As Double
Dim HexBytes(0 To 7) As Byte
Dim i As Integer
Dim x As String
If Len(hNumber) < 16 Then
MsgBox "Hex Value too big"
Exit Function
End If
For i = 0 To 7
HexBytes(i) = Val("&H" & Mid(hNumber, (8 - i) * 2 - 1, 2))
Next i
CopyMemory HEXtoDouble, HexBytes(0), 8
End Function
-
Jun 22nd, 2021, 05:56 PM
#2
Re: Decimal to Hexadecimal in Half instead of Double
Better explain what you want. There is no VB or Automation support for R2 half-precision floats.
Also, that code sucks on many levels. What garbage can did you find it in?
-
Jun 22nd, 2021, 11:11 PM
#3
Re: Decimal to Hexadecimal in Half instead of Double
There actually is a standard for floating-point-hex, but VB6 certainly knows nothing about it. Furthermore, those functions in the OP certainly aren't following the standard.
Here's a page that discusses floating-point-hex. I've only seen it in C (and its derivatives), where the "0x" prefix rather than the "&H" prefix is used.. Basically, the format is: sign0xsignificandpexponent
... where the significand and exponent are hex numbers (0 to F). I've always thought of the "p" as meaning "point" (or decimal point). But the exponent is hex, so it's still a bit difficult to figure out because our brains are so wired for base 10.
I don't know if Windows has an API call to interpret floating-point hex strings or not. I've certainly never seen one, but it wouldn't surprise me if one existed.
-----------
Now, you could certainly do a memory copy, and just report the 8-bytes of a double as an unsigned hex integer. You could do the same for a single. And, if you wanted, you could treat 2-bytes as a floating point "half", but I truly don't see the point in any of that.
As Dil says, Juggler, we really need a narrative explanation of what you're after.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jun 23rd, 2021, 12:27 AM
#4
Re: Decimal to Hexadecimal in Half instead of Double
I suspect the only reason hex comes in is either deep confusion or as a clunky way to pass binary data around as text. i think he's talking about faffing around with half-precision floating-point format (the R2 data type, i.e. 16-bit or 2 byte real data) but I'll admit that's a wild guess.
-
Jun 23rd, 2021, 01:10 AM
#5
Thread Starter
Member
Re: Decimal to Hexadecimal in Half instead of Double
@dilettante;
I am not faffing around ... I am talking about half-precision floating numbers here. The code that I shared converts between unsigned and signed 64-bit double-precision floating-point hexadecimal value in IEEE 754 “signed magnitude hexadecimal” format to 64-bit double-precision floating-point decimal value.
If we modify HexBytes(0 To 7) to HexBytes(0 To 3) and appropriately modify the code, we get the 32-bit single-precision floating-point hexadecimal value.
With hex being a compact version of binary numbers, what I was looking for was to get either the hex or binary equivalent code for half-precision float.
For example, here’s what pi looks like at each precision level of the binary floating-point system:
' — “Double-precision” binary: 0-10000000000-1001001000011111101101010100010001000010110100011000.
' — “Single-precision” binary: 0-10000000-10010010000111111011011.
' — “Half-precision” binary: 0-10000-1001001000.
For example, here’s what pi looks like at each precision level of the hex floating-point system:
' — “Double-precision” hex: 400921FB54442D18.
' — “Single-precision” hex: 40490FDB.
' — “Half-precision” hex: ???.
Esentially, what is the hex for 0-10000-1001001000?
The above hex values were calculated using the code I shared. For cross-reference it (almost) matches the output at https://gregstoll.com/~gregstoll/floattohex/
-
Jun 23rd, 2021, 01:17 AM
#6
Thread Starter
Member
Re: Decimal to Hexadecimal in Half instead of Double
Similarly, the value 12345 with a standard binary of 0011000000111001 has the following output:
64-bit Double: 40C81C8000000000 | 0-10000001100-1000000111001000000000000000000000000000000000000000
32-bit Single: 4640E400 | 0-10001100-10000001110010000000000
16-bit half: ??? | 0-10001-1000000111
Using gregstoll.com 0x4640e400 = 12345 and 0x40c81c8000000000 = 12345. Exact Match.
Also 40c81c8000000000 is the output from the Hex-to-Dec-to-Hex code shared.
-
Jun 23rd, 2021, 10:48 AM
#7
Re: Decimal to Hexadecimal in Half instead of Double
-
Jun 24th, 2021, 08:08 AM
#8
Thread Starter
Member
Re: Decimal to Hexadecimal in Half instead of Double
@The trick; is there a test code on the usage? I am struggling with the Sub usage.
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
|