在xslt中将负十进制转换为十六进制 [英] Convert negative decimal to hexadecimal in xslt

查看:28
本文介绍了在xslt中将负十进制转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问,您能帮我如何在 xslt/xslt 2.0 中将负/正十进制值转换为十六进制吗?

Please, can you help me how to convert negative/positive decimal value to hexadecimal in xslt/xslt 2.0 ?

这是我尝试过的,但不适用于负数/小数,

This is what i tried but does not work with negative numbers/decimals,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template name="ConvertDecToHex">
    <xsl:param name="index" />
    <xsl:if test="$index > 0">
      <xsl:call-template name="ConvertDecToHex">
        <xsl:with-param name="index" select="floor($index div 16)" />
      </xsl:call-template>
      <xsl:choose>
        <xsl:when test="$index mod 16 &lt; 10">
          <xsl:value-of select="$index mod 16" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="$index mod 16 = 10">A</xsl:when>
            <xsl:when test="$index mod 16 = 11">B</xsl:when>
            <xsl:when test="$index mod 16 = 12">C</xsl:when>
            <xsl:when test="$index mod 16 = 13">D</xsl:when>
            <xsl:when test="$index mod 16 = 14">E</xsl:when>
            <xsl:when test="$index mod 16 = 15">F</xsl:when>
            <xsl:otherwise>A</xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

推荐答案

这种转变:

<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="my:my">
 <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:value-of select="my:intToHex(53), my:intToHex(-53)" separator="&#xA;"/>
  </xsl:template>

    <xsl:function name="my:intToHex" as="xs:string">
      <xsl:param name="pInt" as="xs:integer"/>

      <xsl:variable name="vMinusOneHex64" select="18446744073709551615"/>
      <xsl:variable name="vCompl" select=
        "if($pInt ge 0)
           then $pInt
           else $vMinusOneHex64 + $pInt +1"/>
      <xsl:sequence select=
      "if ($vCompl eq 0) 
          then '0' 
          else 
            concat(if ($vCompl gt 16)
                      then my:intToHex($vCompl idiv 16) 
                      else '',
                   substring('0123456789ABCDEF', 
                             ($vCompl mod 16) +1, 
                             1)
                   )"/>
    </xsl:function>
</xsl:stylesheet>

应用于任何源 XML 文档(被忽略)时,会产生所需的结果:

35
FFFFFFFFFFFFFFCB

这篇关于在xslt中将负十进制转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆