ToString() 函数在 VB .NET 中添加一个零?

ToString() function adding a zero in VB .NET?

每当我使用 BigInteger.ToString("X") 方法将 "convert" 一个 BigInteger 值转为 HEX 时,它会出于某些不明原因(对我来说)添加一个额外的零。例如:

Dim val As New BigInteger
Dim res As New String

val = 604462909807314587353089

res = val.ToString("X")

在这种情况下,res 等于:

res: 080000000000000000001

第一个零很麻烦,因为我将这些值传递给某些只允许一定数量的十六进制字符的设备。我当然可以通过额外的一行或两行轻松删除它,但考虑到我的程序也是一个巨大的解析循环,恐怕这样做会延长执行时间。

知道这是从哪里来的吗? 非常感谢你。 :)

前导 0 表示该数字是正数。在有符号整数中,最大位用作符号位,因此它表示负数。

例如:

// Sorry this is in C#
// 0x00 ~ 0x7F is always positive (0~127), no need to add leading 0
// 0x80 ~ 0xFF in signed number would be negative(-128~-1), but in unsigned it will be 128~255
new BigInteger(128).ToString("X") == "080"; // this is positive 128!
new BigInteger(-128).ToString("X") == "80"; // this is negative 128!

请注意,前导 0 帮助您确定它实际上是什么数字。

如果您知道要传递给其他设备的数字位数,那么我建议您将其实际截断/格式化为特定数字,例如:ToString("X20") 将始终格式化为 20数字。 前导 0 应该会影响你计算中的几乎 none,所以你不必担心它。