Convert decimal to ieee745

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.

2 Likes

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

3 Likes

Hallo

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 https://www.openoffice.org/udk/common/man/tools.html#idlc.
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
4 Likes

And for completeness, the third practical option listed in @erAck 's answer. We place in the document (for example, using APSO) the utils.py 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("vnd.sun.star.script:utils.py$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:

Version: 7.0.4.2
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

One small issue related to the Add-in function in LO 7.x.y versions?

In a localized LO installation appears the CompatibilityName instead of the displayed names (if if they are localized or not) in the formula wizzard (in the list of the functions) - when I set the “language” of the Formulas back to “English”.

When that option is not set, then the names are right, even if the Addin has NOT contain localised versions of the Displayed names for the functions.)

It worked fine in the LO 6.x.y versions: always appeared the Displayed names.

Checked in LO 7.4.5, LO 7.5.1. (Same issue with the test of Add-in named DoobieDoo):
.
With NOT English function names:


.
With English function names:

it works the same way (fallback to en - Displayname) in:
sorry was wrong, same issue… fallback to CompatibilityName IF Option [x]english Functionnames is set.

Version: 7.0.4.2
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

Note that this goes farther and farther into the off-topic area :wink: Please file a bug report. This is definitely related to @erAck’s work on tdf#135993.

Reported: 154582 – Add-in function names appears as "Compatibility names" in "English function names" state of the Calc setting.