Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: "Dimitre Novatchev" <dnovatchev@--------->
To:
Date: 7/1/2008 3:17:00 PM
One possible way:

 <xsl:function name="f:hex-to-string" as="xs:string" >
   <xsl:param name="pHex" as="xs:string"/>

   <xsl:variable name="vHexDigits" as="xs:string"
     select="'0123456789ABCDEF'"/>

   <xsl:variable name="vHex" select="upper-case($pHex)"/>

   <xsl:sequence select=
    "if(string-length($pHex) mod 2 ne 0
       or
        string-length(translate($vHex,$vHexDigits,'')) ne 0
        )
        then error()
        else
          codepoints-to-string(
	          (for $i in (1 to string-length($vHex))[. mod 2 = 1],
	               $d1 in substring($vHex,$i,1),
	               $d2 in substring($vHex,$i+1,1)
	
	                  return
	                     16* string-length(substring-before($vHexDigits, $d1))
	                    +
	                      string-length(substring-before($vHexDigits, $d2))
	           )
	                              )

          "
    />
 </xsl:function>



When we test this function in the following transformation:


<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="f xs"
 >
  <xsl:output method="text"/>

	 <xsl:template match="/">
	  <xsl:value-of select="string-to-codepoints('Hello, World!')"/>
	  =================
	  <xsl:value-of select=
	  "codepoints-to-string((72,101,108,108,111,44,32,87,111,114,108,100,33))"/>
	  =================
	  <xsl:value-of select="f:hex-to-string('48656c6c6f2c20576f726c6421')"/>
	 </xsl:template>

 <xsl:function name="f:hex-to-string" as="xs:string" >
   <xsl:param name="pHex" as="xs:string"/>

   <xsl:variable name="vHexDigits" as="xs:string"
     select="'0123456789ABCDEF'"/>

   <xsl:variable name="vHex" select="upper-case($pHex)"/>

   <xsl:sequence select=
    "if(string-length($pHex) mod 2 ne 0
       or
        string-length(translate($vHex,$vHexDigits,'')) ne 0
        )
        then error()
        else
          codepoints-to-string(
	          (for $i in (1 to string-length($vHex))[. mod 2 = 1],
	               $d1 in substring($vHex,$i,1),
	               $d2 in substring($vHex,$i+1,1)
	
	                  return
	                     16* string-length(substring-before($vHexDigits, $d1))
	                    +
	                      string-length(substring-before($vHexDigits, $d2))
	           )
	                              )

          "
    />
 </xsl:function>

</xsl:stylesheet>


the wanted result is produced:

72 101 108 108 111 44 32 87 111 114 108 100 33
	  =================
	  Hello, World!
	  =================
	  Hello, World!


-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play



On Tue, Jul 1, 2008 at 6:42 AM, Steven Hentschel
<steven.hentschel@xxxxxxxxxxx> 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. 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). 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?
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>        !
>        "
>        #
>        $
>        %
>        &
>        &apos;
>        (
>        )
>        *
>        +
>        ,
>        -
>        .
>        /
>        0
>        1
> etc.
>
>
> Regards
>
> Steven Hentschel
> _________________________________________________________________
>
> http://clk.atdmt.com/UKM/go/msnnkmgl0010000002ukm/direct/01/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent