Home > Back-end >  convert hex to ascii characters xslt
convert hex to ascii characters xslt

Time:02-05

I am wondering what the logic for converting hex to ascii characters in XSLT 1.0 is like. I need to convert 496e7465726e616c204d65646963696e65 to Internal Medicine using XSLT 1.0 template.

I have a template to convert ascii to hex and just need help to reverse the logic. Can some XSLT gurus help me with the logic?

  <xsl:output method="xml" indent="yes"/>

  <!-- the next line is all on one line and the before the ! is a space -->
  <xsl:variable name="ascii"> !"#$%&amp;'()* ,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>
  <xsl:variable name="hex" >0123456789ABCDEF</xsl:variable>

  <xsl:template match="/">

    <xsl:variable name="foo" select="'I have $1,001.'"/>

    <result>
      <string>
        <xsl:value-of select="$foo"/>
      </string>
      <hex>
        <xsl:call-template name="recurse-over-string">
          <xsl:with-param name="str" select="$foo"/>
        </xsl:call-template>
      </hex>
    </result>

  </xsl:template>

  <xsl:template name="recurse-over-string">
    <xsl:param name="str"/>   
    <xsl:if test="$str">
      <xsl:variable name="first-char" select="substring($str,1,1)"/>
      <xsl:variable name="ascii-value" select="string-length(substring-before($ascii,$first-char))   32"/>
      <xsl:variable name="hex-digit1" select="substring($hex,floor($ascii-value div 16)   1,1)"/>
      <xsl:variable name="hex-digit2" select="substring($hex,$ascii-value mod 16   1,1)"/>
      <xsl:value-of select="concat($hex-digit1,$hex-digit2)"/>
      <xsl:if test="string-length($str) &gt; 1">
        <xsl:text> </xsl:text>
        <xsl:call-template name="recurse-over-string">
          <xsl:with-param name="str" select="substring($str,2)"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

CodePudding user response:

This is a rather trivial exercise in base conversion:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:param name="str">496e7465726e616c204d65646963696e65</xsl:param>

<xsl:variable name="hex">0123456789abcdef</xsl:variable>
<xsl:variable name="ascii"> !"#$%&amp;'()* ,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>

<xsl:template match="/">
    <acii>
        <xsl:call-template name="hex-to-ascii">
            <xsl:with-param name="str" select="$str"/>
        </xsl:call-template>
    </acii>
</xsl:template>

<xsl:template name="hex-to-ascii">
    <xsl:param name="str"/>   
    <xsl:if test="$str">
        <!-- extract first 2 digits -->
        <xsl:variable name="char1" select="substring($str, 1, 1)"/>
        <xsl:variable name="char2" select="substring($str, 2, 1)"/>
        <!-- get their hex values -->
        <xsl:variable name="val1" select="string-length(substring-before($hex, $char1))"/>
        <xsl:variable name="val2" select="string-length(substring-before($hex, $char2))"/>
        <!-- convert to dec value -->
        <xsl:variable name="dec-value" select="$val1 * 16   $val2"/>
        <!-- get the corresponding ascii character -->
        <xsl:value-of select="substring($ascii, $dec-value - 31, 1)"/>
        <!-- recursive call with the rest of the hex string -->
        <xsl:call-template name="hex-to-ascii">
            <xsl:with-param name="str" select="substring($str, 3)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="utf-8"?>
<acii>Internal Medicine</acii>
  •  Tags:  
  • Related