Public Sub Main(args() As string)
'Inputs are hardcoded
Dim strLength As Integer = 8
Dim longValue As Long = 819
'We're trying to convert Long(819) to Hex String of Length 8
Dim hexString As String = longValue.ToString("X" & strLength)
Console.WriteLine(hexString)
End Sub
我们正在尝试将 Long(819) 转换为长度为 8 的十六进制字符串。执行上面的代码将产生输出:00000333
我们可以通过在线编译器执行 VB 代码 -
XML
<input>
<decimal>0</decimal>
<decimal>1</decimal>
<decimal>9</decimal>
<decimal>10</decimal>
<decimal>15</decimal>
<decimal>16</decimal>
<decimal>34</decimal>
<decimal>96</decimal>
<decimal>178</decimal>
<decimal>506</decimal>
<decimal>1308</decimal>
<decimal>9734</decimal>
<decimal>53789</decimal>
<decimal>786120</decimal>
</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="math">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/input">
<output>
<xsl:for-each select="decimal">
<xsl:variable name="dec" select="."/>
<xsl:variable name="hex-values" select="for $i in 0 to 7 return ($dec idiv math:pow(16, $i)) mod 16"/>
<hex>
<xsl:value-of select="for $j in reverse($hex-values) return codepoints-to-string($j + (if($j > 9) then 55 else 48))" separator=""/>
</hex>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>
<hex>00000000</hex>
<hex>00000001</hex>
<hex>00000009</hex>
<hex>0000000A</hex>
<hex>0000000F</hex>
<hex>00000010</hex>
<hex>00000022</hex>
<hex>00000060</hex>
<hex>000000B2</hex>
<hex>000001FA</hex>
<hex>0000051C</hex>
<hex>00002606</hex>
<hex>0000D21D</hex>
<hex>000BFEC8</hex>
</output>
当然,如果输出固定 8 个十六进制数字,则输入值预计在 0 到 4294967295 的范围内。