I am trying to convert decimal to ieee754 format
Any example of what exactly you are trying to achieve?
Internally, Calc uses IEEE 754 64-bit double for all numeric data, so any decimal input is converted to that anyway, but that seems not what you’re asking…
The short version of the question suggested you (the questioner) knew what you were asking, and that “745” just was a typo (for “754”). In addition everybody expected that of every specification contained in IEEE 754 only ‘Double’ could be addressed because it’s the one format actually used by current PC Floating-Point units.
What came afterwards could only amount to uncomprehending astonishment.
If you actually want to convert a decimal number (given as a string?) to the 64-bit representation specified as IEEE 754 Double
, please tell so explicitly. If the target is a different format, tell which one in an understandable way. If you don’t know yourself, the question makes no sense.
The precise standard I am looking for is ieee 751 binary single precision
I want to convert for example 85.0 converts to Ox42aa
Do you know somebody who knows somebody who might be able to explain this answer to me?
Is it a two byte representation of the floating point numbers?
I had use the 4 byte (single precision) numbers for the ModBus communication, and a special 3 byte number representation one for the Honeywell DE (Digital Enchanced) protocoll. And of course I know the 8 byte (double precision) version, what the LO Calc uses.
.
But i never heard about the 2 byte representation of the floating point numbers. Where and how you want to use it?
That example is incomplete and just the upper two bytes of a 32-bit float where 85 decimal is represented as 0x42AA0000 (binary 0 10000101 01010100000000000000000).
Here is a sample file embedded some StarBasic functions to convert Single and Double precision numbers to Hexadecimal strings.
The macros use the Pipe technology.
Pipe2.ods (18.5 KB)
(Fixed - I hope it)
Here is a sample file
Just needs to replace ClosePipe
with CloseMyPipe
in one place (in PipeSingle2HexStr
).
A possible problem could be handling of endianness on some (less common) platforms. But hopefully, the XData*Stream
will handle that cross-platform.
Sorry, I just tried to copy the macros from the MyMacros, but with different name… (The “original name” was ClosePipe in the MyMacros)
I need to do the conversion within the spreadsheet
Please do not use the Answer or Suggest a solution field for comments that are not an answer to the original question / solution to the problem, use Comment instead. Thanks.
The conversion itself is fairly simple in Python:
def float_to_hex(f):
return hex(struct.unpack('<I', struct.pack('<f', f))[0])
def double_to_hex(f):
return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
(taken from python - How to convert a float into hex - Stack Overflow).
You’d need to either create a Calc Add-In extension of that, or glue Python calls into a BASIC user defined function (UDF) callable from Calc. You might get some ideas from Basic to Python.
See also Macros/Python Guide/Calling Python from Basic - The Document Foundation Wiki.
Here is an extension built using this method:
pyFloatToHex.oxt.ods (2.6 KB) (updated to include the improvements from @karolus)
Remove the “ODS” extension to have pyFloatToHex.oxt
, and use FLOATTOIEEE754HEX
and DOUBLETOIEEE754HEX
.
This gives the same results as the nice macro by @Zizi64 in #13. The advantage of the extension is much better performance (about 100 times faster on my system: 1 000 000 random numbers take ~1000 s with the macro, ~11 s with the extension). The advantage of the macro is its self-containment (the file with the macro is all that you need, no matter on which system you open it).
Please make sure you compare comparable things - i.e., please define the padding and width
I can understand most of the files in your extension. But i have not any clue how to create/generate a XPyFloatToHex.rdb file. There is a description for this task somewhere? …Maybe I asked it in the past…
I want to learn about creating Python extensions.
please define the padding and width
sofar it makes no difference in the output if I use …:08X or …:X ?? but the first variant is slower!