如何使用 xslt 1 将负十进制转换为十六进制 [英] How to convert a negative decimal into hexadecimal using xslt 1

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

问题描述

我想使用 xslt 1.0 将负十进制和正十进制转换为十六进制.

I would like to convert negative and positive decimal into hexadecimal using xslt 1.0.

已经有一个与此问题相关的主题此处使用 xslt 2.0 给出答案.

There's already a topic related to this question here but the answer is given using xslt 2.0.

我尝试使用 xslt 1.0 重现模板,但它总是返回一个空值.

I tried to reproduce the template using xslt 1.0 but it always returns an empty value.

    <xsl:template name="convertDecToHex">
    <xsl:param name="pInt" />
    <xsl:variable name="vMinusOneHex64"><xsl:number>18446744073709551615</xsl:number></xsl:variable>
    <xsl:variable name="vCompl">
        <xsl:choose>
            <xsl:when test="$pInt &gt; 0">
                <xsl:value-of select="$pInt" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$vMinusOneHex64 + $pInt + 1" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="vCompl = 0">
            <xsl:text>0</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:choose>
                <xsl:when test="vCompl &gt; 16">
                    <xsl:variable name="result">
                        <xsl:call-template name="convertDecToHex">
                            <xsl:with-param name="pInt" select="$vCompl div 16" />
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:value-of select="concat($result,substring('0123456789ABCDEF',($vCompl div 16) + 1,1))" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('',substring('0123456789ABCDEF',($vCompl div 16) + 1,1))" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

你能帮我让它工作吗?

推荐答案

在纯 XSLT 1.0 中将十进制数转换为 32 位有符号十六进制数:

To convert a decimal number to 32-bit signed hexadecimal in pure XSLT 1.0:

<xsl:template name="dec2signedhex">
    <xsl:param name="decimal"/>
    <xsl:variable name="n">
        <xsl:choose>
            <xsl:when test="$decimal &lt; 0">
                <xsl:value-of select="$decimal + 4294967296"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$decimal"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="q" select="floor($n div 16)"/>
    <xsl:if test="$q">
        <xsl:call-template name="dec2signedhex">
            <xsl:with-param name="decimal" select="$q"/>
        </xsl:call-template>
    </xsl:if>
    <xsl:value-of select="substring('0123456789ABCDEF', $n mod 16 + 1, 1)"/>
</xsl:template>

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

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