xsl-list
[Top] [All Lists]

RE: [xsl] Binary to Hex conversion

2007-11-20 15:18:02
Here's mine:

<stylesheet
  xmlns      = "http://www.w3.org/1999/XSL/Transform";
  xmlns:str  = "http://exslt.org/strings";
  xmlns:math = "http://exslt.org/math";
  version="1.0"
  exclude-result-prefixes="str math">

  <variable name="str:digits"     select="'0123456789'"
/>
  <variable name="str:alphabet"
select="'abcdefghijklmnopqrstuvwxyz'"/>
  <variable name="str:ALPHABET"
select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
  <variable name="math:base-list" select="concat($str:digits,
$str:alphabet, $str:ALPHABET)"/>

  <template name="math:base-convert">
    <param name="from-base"/>
    <param name="to-base"/>
    <param name="value"/>
    <call-template name="math:decimal-to-base">
      <with-param name="base" select="$to-base"/>
      <with-param name="value">
        <call-template name="math:base-to-decimal">
          <with-param name="base" select="$from-base"/>
          <with-param name="value">
            <choose>
              <when test="$from-base &gt; 36">
                <value-of select="$value"/>
              </when>
              <otherwise>
                <value-of select="translate($value, $str:ALPHABET,
$str:alphabet)"/>
              </otherwise>
            </choose>
          </with-param>
        </call-template>
      </with-param>
    </call-template>
  </template>

  <template name="math:base-to-decimal">
    <param name="base"/>
    <param name="value"/>
    <variable name="base-recurse">
      <choose>
        <when test="string($value)">
          <call-template name="math:base-to-decimal">
            <with-param name="base" select="$base"/>
            <with-param name="value" select="substring($value, 1,
string-length($value) - 1)"/>
          </call-template>
        </when>
        <otherwise>0</otherwise>
      </choose>
    </variable>
    <value-of select="string-length(substring-before($math:base-list,
substring($value, string-length($value)))) + $base * $base-recurse"/>
  </template>

  <template name="math:decimal-to-base">
    <param name="base"/>
    <param name="value" select="0"/>
    <if test="number($value)">
      <call-template name="math:decimal-to-base">
        <with-param name="base" select="$base"/>
        <with-param name="value" select="floor($value div $base)"/>
      </call-template>
      <value-of select="substring($math:base-list, ($value mod $base) +
1, 1)"/>
    </if>
  </template>

</stylesheet>

~ Scott


-----Original Message-----
From: Jeff Hooker [mailto:jeff(_at_)itwriting(_dot_)ca] 
Sent: Tuesday, November 20, 2007 4:13 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Binary to Hex conversion

I'm sure I've seen this discussed somewhere, but the archive appears
dry.

I have a binary value that is a result of concatenating the default
values for a series of bits, and now I need to convert the complete
value to hex so that I can display it as the "reset value" for the whole
register. 

eg.

<bit>
    <bit_default>0</bit_default>
</bit>
<bit>
    <bit_default>1</bit_default>
</bit>
<bit>
    <bit_default>X</bit_default> <!--null bit-->
</bit>
<bit>
    <bit_default>1</bit_default>
</bit>

I gather them into a variable, substitute the Xs for 0s to allow for
conversion to hex, and then..stop. I've searched for any math extensions
that would allow an easy conversion to hex, but have come up dry, and
I'm simply not up to the task of creating a recursive template to
perform the conversion; on an 8000 page document it would likely still
be running over the Christmas break. 

A register can be hundreds of bits in length, hence my desire to present
the default settings in a more compressed format than their binary
values.

Any suggestions?

Thanks,
Jeff.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>