How to convert a negative decimal into hexadecimal using xslt 1

我的未来我决定 提交于 2020-01-11 13:47:13

问题


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

There's already a topic related to this question here but the answer is given using xslt 2.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>

Could you help me making it work?


回答1:


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>


来源:https://stackoverflow.com/questions/46280843/how-to-convert-a-negative-decimal-into-hexadecimal-using-xslt-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!