Convert decimal to ieee745

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).



1 Like

Please make sure you compare comparable things - i.e., please define the padding and width :slight_smile:

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.

1 Like

sofar it makes no difference in the output if I use …:08X or …:X ?? but the first variant is slower!

@karolus thanks, updated to use that syntax.

@Zizi64: the older method is described in
But I used the newer unoidl-write: from the SDK command prompt, I run

unoidl-write "C:\Program Files\LibreOffice\program\types.rdb" D:\pyFloatToHex\XPyFloatToHex.idl D:\pyFloatToHex\XPyFloatToHex.rdb

And for completeness, the third practical option listed in @erAck 's answer. We place in the document (for example, using APSO) the module containing the functions from @karolus 's answer.
To the Standard library, the Basic module:

Option Explicit

Global oScriptDTH As Object

' Returns a string of 16 hexadecimal digits corresponding to
' the internal representation of double.
Function DoubleToHex(ByVal d As Double) As String
  If oScriptDTH Is Nothing Then
    oScriptDTH = ThisComponent.getScriptProvider.getScript("$doub_hex?language=Python&location=document")
  End If  
  DoubleToHex=oScriptDTH.invoke(Array(d), array(), array())
End Function
1 Like

sorry…one more suggestion:

    @staticmethod # with staticmethod …
    def FloatToIEEE754Hex( f ): # … no  ***self***argument
1 Like

Thank you!
The unoidl-write works for me in the 7.4.6 version of the LO.

Note that unoidl-write format is only supported since LO 4.1; I assume that AOO would not support it. The extensions created using the new tool would need a LibreOffice-minimal-version with value of no less than 4.1.

Note also that the older idlc tool is discontinued in LO SDK since 7.5.

Are you sure with »since LO 4.1« ?
with me:

Build ID: 00(Build:2)
CPU threads: 4; OS: Linux 6.1; UI render: default; VCL: gtk3
Locale: de-DE (de_DE.UTF-8); UI: de-DE
Raspbian package version: 1:7.0.4-4+rpi1+deb11u3
Calc: threaded

WITH installed sdk, I cannot find any hint to »unoidl-write« ?

Yes I’m sure that that format is supported since then (which was what I wrote); yet, the tool itself wasn’t included in the SDK until commit 40f2aee6584eafcf4cd1d95fcf1f775e5435440d in 2022.

1 Like

ok thanks for the clarification!

This looks interesting, how is it used? I have upgraded to latest version of Libreoffice.

Just download this and store it without ».ods«
doubleclick the File … install it … restart LO and use the functions