tags:

views:

324

answers:

1

using xsl 2.0, how would you convert an xs:float value to a hex-string representation of its binary value? i have no problem doing this for an integer (divide by 16 recursively and concatenate chars 0-9A-F), but float/double is stumping me.

<xsl:function name="my:float-to-hex" as="xs:string">
    <xsl:param name="in" as="xs:float"/>
    <xsl:sequence select="magic-here($in)"/>
</xsl:function>

a valid answer may be, "this is not possible," and that would be valuable to me if it were true. i'm open to other suggestions, but please don't depart from XSL. i am fully aware that there are more than one ways to skin this cat.

to clarify, the expected output would be the same as the output from this C code:

float f = 28.25f;
char *ptr = &f;
for(int i = 0; i < sizeof(float); i++) { printf("%02X", *(ptr + i)); }
A: 

I would go out on a limb and say that this is impossible in vanilla XSLT. With access to user-definable extension functions it is easy to offshore it to some language better suited, as you already indicated yourself by magic-here($in).

I don't think XSLT has a way to cast a number into a byte array. I can't point out a relevant section in the spec to prove it, though. Maybe someone else digs up the right paragraph.

XSLT is supposed to be deterministic and side-effects free, and as such it is highly unlikely that it provides a native way to access implementation specific details like the internal number representation.

Tomalak
i suspect that you are right. in fact, i had already come close to the same conclusion myself, but i wanted a second opinion. i've done quite a bit of reading on xslt and on the surface it is billed as capable of converting from xml to xml, xml to anything, and anything to xml; the latter two being more difficult, but possible. however, i discovered that "to anything" should really be "to text" and the level of difficulty of going from "anything to xml" is very high. Realizing that Saxon is pretty much the only XSL 2.0 tool available and the licensing is limited, I'm not impressed.
rev
XSLT is an XML transformation language. Hence the name. It does not do low-level bit twiddling because that's not in the scope of the language. Binary representations of things are different in different environments, yielding the possibility of unpredictable/non-deterministic results. XSLT is turing-complete (even 1.0), but if it is painful to do a specific thing in XSLT, then chances are that it is just not the right tool for that job. And yes "anything" only means "text, formatted to your liking".
Tomalak
thanks for your input - it was valuable and very useful.
rev