XML technologies / XSLT / XSLT Cookbook / Numbers and calculations / Converting between number bases

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need to convert strings representing numbers from one base to another.

Solution

This general solution converts between any bases from 2 to 36. Two global variables encode each base-36 character’s value as offsets into a string, one for uppercase and one for lowercase:

<xsl:variable name="ckbk:base-lower" select="'0123456789abcdefghijklmnopqrstuvwxyz'"/>    
<xsl:variable name="ckbk:base-upper" select="'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>    
<xsl:template name="ckbk:convert-base">
  <xsl:param name="number"/>
  <xsl:param name="from-base"/>
  <xsl:param name="to-base"/>  
  <xsl:variable name="number-base10">
    <xsl:call-template name="ckbk:convert-to-base-10">
      <xsl:with-param name="number" select="$number"/>
      <xsl:with-param name="from-base" select="$from-base"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:call-template name="ckbk:convert-from-base-10">
    <xsl:with-param name="number" select="$number-base10"/>
    <xsl:with-param name="to-base" select="$to-base"/>
  </xsl:call-template>
</xsl:template>

This template splits the problem into conversion to and from base 10, easier because it is the native base of XPath numbers.

Template ckbk:convert-to-base-10 normalizes input to lowercase, treating hexadecimal ffff like FFFF as convention requires. Two error checks ensure the base is supported and digits valid for it. The trivial base-10-to-base-10 case is also handled:

<xsl:template name="ckbk:convert-to-base-10">
  <xsl:param name="number"/>
  <xsl:param name="from-base"/>  
  <xsl:variable name="num" select="translate($number,$ckbk:base-upper, $ckbk:base-lower)"/>
  <xsl:variable name="valid-in-chars" select="substring($ckbk:base-lower,1,$from-base)"/>  
  <xsl:choose>
    <xsl:when test="$from-base &lt; 2 or $from-base &gt; 36">NaN</xsl:when>
    <xsl:when test="not($num) or translate($num,$valid-in-chars,'')">NaN</xsl:when>
    <xsl:when test="$from-base = 10">
      <xsl:value-of select="$number"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="ckbk:convert-to-base-10-impl">
        <xsl:with-param name="number" select="$num"/>
        <xsl:with-param name="from-base" select="$from-base"/>
        <xsl:with-param name="from-chars" select="$valid-in-chars"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

After error checking, another recursive template performs conversion. It gets each character’s decimal value from its position in the supplied string. Recursion multiplies by the base and adds the first character’s value until only one character remains:

<xsl:template name="ckbk:convert-to-base-10-impl">
  <xsl:param name="number"/>
  <xsl:param name="from-base"/>
  <xsl:param name="from-chars"/>  
  <xsl:param name="result" select="0"/>  
  <xsl:variable name="value" select="string-length(substring-before($from-chars,substring($number,1,1)))"/>  
  <xsl:variable name="total" select="$result * $from-base + $value"/>  
  <xsl:choose>
    <xsl:when test="string-length($number) = 1">
      <xsl:value-of select="$total"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="ckbk:convert-to-base-10-impl">
        <xsl:with-param name="number" select="substring($number,2)"/>
        <xsl:with-param name="from-base" select="$from-base"/>
        <xsl:with-param name="from-chars" select="$from-chars"/>
        <xsl:with-param name="result" select="$total"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Conversion from base 10 also separates error checking from conversion:

<xsl:template name="ckbk:convert-from-base-10">
  <xsl:param name="number"/>
  <xsl:param name="to-base"/>  
  <xsl:choose>
    <xsl:when test="$to-base &lt; 2 or $to-base &gt; 36">NaN</xsl:when>
    <xsl:when test="number($number) != number($number)">NaN</xsl:when>
    <xsl:when test="$to-base = 10">
      <xsl:value-of select="$number"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="ckbk:convert-from-base-10-impl">
        <xsl:with-param name="number" select="$number"/>
        <xsl:with-param name="to-base" select="$to-base"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Conversion selects each digit from table $ckbk:base-lower using the remainder, or modulo, after division by $to-base. Recurse on the remaining integer part, placing the digit before the result, until the number is smaller than the base:

<xsl:template name="ckbk:convert-from-base-10-impl">
  <xsl:param name="number"/>
  <xsl:param name="to-base"/>
  <xsl:param name="result"/>  
  <xsl:variable name="to-base-digit" select="substring($ckbk:base-lower,$number mod $to-base + 1,1)"/>
  <xsl:choose>
    <xsl:when test="$number &gt;= $to-base">
      <xsl:call-template name="ckbk:convert-from-base-10-impl">
        <xsl:with-param name="number" select="floor($number div $to-base)"/>
        <xsl:with-param name="to-base" select="$to-base"/>
        <xsl:with-param name="result" select="concat($to-base-digit,$result)"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="concat($to-base-digit,$result)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Discussion

Base conversion is common and familiar to most developers. Many languages support it natively, but XSLT does not. XPath 1.0 and XSLT 1.0 also lack a way to obtain a character’s integer Unicode value, making conversion harder. In XPath 2.0, functions string-to-codepoints and codepoints-to-string are available. Otherwise, use strings as lookup tables: inefficient, but adequate for most conversions.

The code assumes bases above 10 use consecutive letters for digits above 9. For unconventional encodings, adapt the lookup strings. Add further characters to extend beyond base 36 and digits above Z.

  

 

 

 

back to top