在 XSLT 中将十六进制数转换为整数 [英] Convert a hexadecimal number to an integer in XSLT

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

问题描述

我需要将一个十六进制值转换为一个整数值,我该如何使用 XSLT 做到这一点?

I need to convert a hexadecimal value to an integer value, how can I do that using XSLT?

比如输入是十六进制的FF,我的输出应该是255.

For example, if the input is hexadecimal FF, my output should be 255.

推荐答案

在纯 XSLT 1.0 中将十六进制转换为十进制:

<xsl:template name="hex2num">
    <xsl:param name="hex"/>
    <xsl:param name="num" select="0"/>
    <xsl:param name="MSB" select="translate(substring($hex, 1, 1), 'abcdef', 'ABCDEF')"/>
    <xsl:param name="value" select="string-length(substring-before('0123456789ABCDEF', $MSB))"/>
    <xsl:param name="result" select="16 * $num + $value"/>
    <xsl:choose>
        <xsl:when test="string-length($hex) > 1">
            <xsl:call-template name="hex2num">
                <xsl:with-param name="hex" select="substring($hex, 2)"/>
                <xsl:with-param name="num" select="$result"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$result"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

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

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