强制整数为两位数 - VB

时间:2014-08-29 12:38:32

标签: vb.net integer

我有一个变量,它包含一个字符串"02100030000000000000000000D5010008D501000804",我用字节分隔字符串

For i As Integer = 1 To (stringReader.Length - 1) Step 2
                'Get the successive 2-character substrings, parse as bytes and add to total
                Dim b As Byte = Byte.Parse(stringReader.Substring(i, 2), NumberStyles.AllowHexSpecifier)
                sum = sum + CInt(b)

            Next    

我将字符串转换为直接整数.g :(字符串" 10"到Integer10和)。这很好。但每当我转换字符串" 02"到Integer,我只得到Integer(2),我需要整数(02)。我该怎么办?

我的代码是:

stringReader = fileReader.ReadLine()      
byt = stringReader(1) + stringReader(2)      

stringreader包含"100030000000000000000000D5010008D501000804"

之类的内容

字节分离

For i As Integer = 1 To (stringReader.Length - 1) Step 2
                'Get the successive 2-character substrings, parse as bytes and add to total
                Dim b As Byte = Byte.Parse(stringReader.Substring(i, 2), NumberStyles.AllowHexSpecifier)
                sum = sum + CInt(b)

            Next  

1 个答案:

答案 0 :(得分:6)

您可以使用

number.ToString("D2") 

其中number是一个整数类型,如System.Int32Integer)。

进一步阅读:Standard Numeric Format Strings: The Decimal ("D") Format Specifier

如果你有String,你也可以使用String.PadLeft

"2".PadLeft(2, "0"c)  ' -> "02"