Re: [xsl] decoding hex string to ascii or UTF-8

Subject: Re: [xsl] decoding hex string to ascii or UTF-8
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 01 Jul 2008 10:34:59 -0400
At 2008-07-01 14:42 +0100, Steven Hentschel wrote:
I'm currently trying to work out the best way to
convert a string of hex into the characters represented by each hex byte.

Which isn't what your subject line says ... your subject line is talking about an encoding which is different than your opening sentence which is talking about abstract characters. I'm answering the sentence, not the subject line.

I'm using xslt 2.0. So far I've come up with a
function that simply does a lookup to from the
hex value to the ascii character that hex value represents (see below).

My mailer corrupted your mail message so I could not see your code.


Can anyone suggest a better way? Is there a way
(without using a huge lookup to achieve a
similar sort of  thing for UTF-8 chars?

Mechanically calculating the hexadecimal code point and translating it is labourious but straightforward. Below is an example, but I cannot compare it to yours since I cannot see the way you've approached it.

I hope this helps.

. . . . . . . . . . . . Ken

t:\ftemp>type hex2ascii.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xsd="http://www.w3.org/2001/XMLSchema";
                xmlns:k="urn:x-Ken"
                exclude-result-prefixes="xsd k"
                version="2.0">

<xsl:output method="text" encoding="iso-8859-1"/>

<xsl:variable name="hexdigits"
   select="('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F')
"/>

<xsl:template match="/">
  <xsl:value-of select="k:hex2ascii('004400E9006A00E0')"/>
</xsl:template>

<xsl:function name="k:hex2ascii" as="xsd:string?">
  <xsl:param name="hex" as="xsd:string"/>
  <xsl:variable name="codepoints" as="xsd:integer*">
    <xsl:for-each select="1 to ( string-length($hex) idiv 4 )">
      <xsl:sequence select=
       "(index-of($hexdigits,substring($hex,(. - 1) * 4 + 1,1)) - 1 ) * 4096
+
        (index-of($hexdigits,substring($hex,(. - 1) * 4 + 2,1)) - 1 ) *  256
+
        (index-of($hexdigits,substring($hex,(. - 1) * 4 + 3,1)) - 1 ) *   16
+
        (index-of($hexdigits,substring($hex,(. - 1) * 4 + 4,1)) - 1 )"/>
    </xsl:for-each>
  </xsl:variable>
  <xsl:sequence select="codepoints-to-string($codepoints)"/>
</xsl:function>

</xsl:stylesheet>
t:\ftemp>xslt2 hex2ascii.xsl hex2ascii.xsl hex2ascii.txt

t:\ftemp>cat hex2ascii.txt
Dij`
t:\ftemp>

--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread