[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: decoding hex string to ascii or UTF-8

Subject: Re: decoding hex string to ascii or UTF-8
From: Steven Hentschel <steven.hentschel@xxxxxxxxxxx>
Date: Wed, 2 Jul 2008 09:35:34 +0100
Re:  decoding hex string to ascii or UTF-8
Many thanks, I wasn't aware of the codepoints-to-string() function which does
exactly what I was after.

My original example was corrupted because I'm sending via hotmail and forgot
to escape the angle brackets!! As you say, mechanically calculating the
hexadecimal code
point and translating it is labourious but straightforward.

<!--function  hex:hex2string() recursively translates hex into ascii, uses
hex:hex2char to translate individual bytes-->
<xsl:function name="hex:hex2string" as="xs:string">
<xsl:param name="hexstring" as="xs:string"/>
<xsl:choose>
  <xsl:when test="string-length($hexstring) gt 2">
    <xsl:value-of select="concat(hex:hex2char(substring($hexstring, 1, 2)),
hex:hex2string(substring($hexstring, 3)))"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="hex:hex2char(substring($hexstring, 1, 2))"/>
  </xsl:otherwise>
</xsl:choose>
</xsl:function>

<!--function  hex:hex2char() translates a byte as hex into an ascii char-->
<xsl:function name="hex:hex2char" as="xs:string">
<xsl:param name="hexchar" as="xs:string"/>
<xsl:value-of>
      <xsl:choose>
        <xsl:when test="substring($hexchar, 1, 2) = '20'"><xsl:text>
</xsl:text></xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '21'">!</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '22'">"</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '23'">#</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '24'">$</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '25'">%</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '26'">&</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '27'">&apos;</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '28'">(</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '29'">)</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '2A'">*</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '2B'">+</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '2C'">,</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '2D'">-</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '2E'">.</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '2F'">/</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '30'">0</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '31'">1</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '32'">2</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '33'">3</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '34'">4</xsl:when>
        <xsl:when test="substring($hexchar, 1, 2) = '35'">5</xsl:when>

etc

Steven Hentschel
_________________________________________________________________

http://clk.atdmt.com/UKM/go/msnnkmgl0010000002ukm/direct/01/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.