我有一个变量,它包含一个字符串"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
答案 0 :(得分:6)
您可以使用
number.ToString("D2")
其中number
是一个整数类型,如System.Int32
(Integer
)。
进一步阅读:Standard Numeric Format Strings: The Decimal ("D") Format Specifier
如果你有String
,你也可以使用String.PadLeft
:
"2".PadLeft(2, "0"c) ' -> "02"