I need to convert hex to a decimal in VB.NET. Found several examples in C#, but when I tried to convert to VB.NET I was not successful. An example of a hexadecimal number that I am trying to convert is "A14152464C203230304232323020572F544947455234352E".
我需要在VB.NET中将十六进制转换为十进制。在C#中找到了几个例子,但是当我尝试转换为VB.NET时,我没有成功。我想要转换的十六进制数的示例是“A14152464C203230304232323020572F544947455234352E”。
9 个解决方案
#1
6
That is a 24-byte (192-bit) number; what value are you expecting?
这是一个24字节(192位)的数字;你期待什么价值?
Note that you can use Convert
to do a lot of the grungy work here - for example (in C#):
请注意,您可以使用Convert在这里做很多蹩脚的工作 - 例如(在C#中):
string hex = "A14152464C203230304232323020572F544947455234352E";
byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length ; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2,2), 16);
}
How you get from raw
to a number depends on what you think the number is...
如何从原始到数字取决于您认为数字是什么...
Visual Basic translation courtesy of .NET Reflector (although the "-1" looks odd):
Visual Basic翻译由.NET Reflector提供(虽然“-1”看起来很奇怪):
Dim hex As String = "A14152464C203230304232323020572F544947455234352E"
Dim raw As Byte() = New Byte((hex.Length / 2) - 1) {}
Dim i As Integer
For i = 0 To raw.Length - 1
raw(i) = Convert.ToByte(hex.Substring((i * 2), 2), &H10)
Next i
#2
15
For hex values which don't actually require a bignum class to work with, you can use the normal conversion function but prefix the number with "&H". VB interprets "&H" in the text as meaning "this is a hex number", just like it does in code.
对于实际上不需要使用bignum类的十六进制值,可以使用正常转换函数,但在数字前面加上“&H”。 VB将文本中的“&H”解释为“这是十六进制数”,就像在代码中一样。
dim n = Cint("&H" & text)
#3
4
You can use the Val
function in Visual Basic to convert a hexadecimal value to a decimal value. This is done by prefixing the string with "&H"
, to tell Visual Basic that this is a hexadecimal value, and then convert that into a number.
您可以使用Visual Basic中的Val函数将十六进制值转换为十进制值。这是通过在字符串前加上“&H”来完成的,告诉Visual Basic这是一个十六进制值,然后将其转换为数字。
Dim Value As Integer = Val("&H" & YourHexadecimalStringHere)
#4
1
This will convert your string into an array of bytes:
这会将您的字符串转换为字节数组:
Dim hex As String = "A14152464C203230304232323020572F544947455234352E"
Dim len As Integer = hex.Length \ 2
Dim data(len - 1) As Byte
For i As Integer = 0 to len - 1
data(i) = Convert.ToByte(hex.Substring(i * 2, 2), 16)
Next
#5
1
Private Function toByte(ByVal Shex As String) As List(Of Byte)
Const cvtCH As Integer = 2
Dim retval As New List(Of Byte)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToByte(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Function toU32(ByVal Shex As String) As List(Of UInt32)
Const cvtCH As Integer = 8
Dim retval As New List(Of UInt32)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToUInt32(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Function toU64(ByVal Shex As String) As List(Of UInt64)
Const cvtCH As Integer = 16
Dim retval As New List(Of UInt64)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToUInt64(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'unsigned 32 bit max = FFFFFFFF
'unsigned 64 bit max = FFFFFFFF
'signed 32 bit max = 7FFFFFFF
'signed 64 bit max = 7FFFFFFF
Dim hexS As String = "A14152464C203230304232323020572F544947455234352E"
Dim hexS2 As String = "A14152464C203230304232323020572F54494745523435"
toByte(hexS)
toU32(hexS)
toU64(hexS)
End Sub
#6
1
Dim hex As String
hex = "A14152464C203230304232323020572F544947455234352E"
Dim dec As Long
Dim hexpart As String
For x As Integer = 1 To (hex.Length / 2)
hexpart = hex.Substring((x * 2) - 2, 2)
dec = Int32.Parse(hexpart, System.Globalization.NumberStyles.HexNumber)
Debug.Print("Hex = " + hex + ",HexPart = " + hexpart + ", Dec = " + dec.ToString + Environment.NewLine)
Next
This won't work for Decimal and the Hex is too long for integer... but you get the idea. You could split it and recombine.
这对于Decimal不起作用,Hex对整数来说太长了......但是你明白了。你可以将它拆分并重新组合。
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = A1, Dec = 161
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 41, Dec = 65
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 52, Dec = 82
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 46, Dec = 70
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 4C, Dec = 76
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 20, Dec = 32
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 42, Dec = 66
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 20, Dec = 32
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 57, Dec = 87
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 2F, Dec = 47
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 54, Dec = 84
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 49, Dec = 73
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 47, Dec = 71
#7
1
Write one yourself.
自己写一个。
You will need to tokenize the string, then start from the right, and work your way left.
您将需要对字符串进行标记,然后从右侧开始,然后继续前进。
int weight = 1;
While Looping
{
If (token(i) == "F") { DecimalValue += 15 * weight; }
If (token(i) == "E") { DecimalValue += 14 * weight; }
If (token(i) == "D") { DecimalValue += 13 * weight; }
If (token(i) == "C") { DecimalValue += 12 * weight; }
If (token(i) == "B") { DecimalValue += 11 * weight; }
If (token(i) == "A") { DecimalValue += 10 * weight; }
else { DecimalValue += token(i) * weight; }
weight = weight * 16;
}
Something like that.
这样的事情。
#8
1
Tried the others replies and this was the one that worked for me:
试过其他人回复,这是对我有用的:
Convert.ToInt32(yourHEXString, 16)
#9
0
Dim hex As String = "A1B2C3D4"
Dim int As Integer = Val("&H" & hex)
#1
6
That is a 24-byte (192-bit) number; what value are you expecting?
这是一个24字节(192位)的数字;你期待什么价值?
Note that you can use Convert
to do a lot of the grungy work here - for example (in C#):
请注意,您可以使用Convert在这里做很多蹩脚的工作 - 例如(在C#中):
string hex = "A14152464C203230304232323020572F544947455234352E";
byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length ; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2,2), 16);
}
How you get from raw
to a number depends on what you think the number is...
如何从原始到数字取决于您认为数字是什么...
Visual Basic translation courtesy of .NET Reflector (although the "-1" looks odd):
Visual Basic翻译由.NET Reflector提供(虽然“-1”看起来很奇怪):
Dim hex As String = "A14152464C203230304232323020572F544947455234352E"
Dim raw As Byte() = New Byte((hex.Length / 2) - 1) {}
Dim i As Integer
For i = 0 To raw.Length - 1
raw(i) = Convert.ToByte(hex.Substring((i * 2), 2), &H10)
Next i
#2
15
For hex values which don't actually require a bignum class to work with, you can use the normal conversion function but prefix the number with "&H". VB interprets "&H" in the text as meaning "this is a hex number", just like it does in code.
对于实际上不需要使用bignum类的十六进制值,可以使用正常转换函数,但在数字前面加上“&H”。 VB将文本中的“&H”解释为“这是十六进制数”,就像在代码中一样。
dim n = Cint("&H" & text)
#3
4
You can use the Val
function in Visual Basic to convert a hexadecimal value to a decimal value. This is done by prefixing the string with "&H"
, to tell Visual Basic that this is a hexadecimal value, and then convert that into a number.
您可以使用Visual Basic中的Val函数将十六进制值转换为十进制值。这是通过在字符串前加上“&H”来完成的,告诉Visual Basic这是一个十六进制值,然后将其转换为数字。
Dim Value As Integer = Val("&H" & YourHexadecimalStringHere)
#4
1
This will convert your string into an array of bytes:
这会将您的字符串转换为字节数组:
Dim hex As String = "A14152464C203230304232323020572F544947455234352E"
Dim len As Integer = hex.Length \ 2
Dim data(len - 1) As Byte
For i As Integer = 0 to len - 1
data(i) = Convert.ToByte(hex.Substring(i * 2, 2), 16)
Next
#5
1
Private Function toByte(ByVal Shex As String) As List(Of Byte)
Const cvtCH As Integer = 2
Dim retval As New List(Of Byte)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToByte(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Function toU32(ByVal Shex As String) As List(Of UInt32)
Const cvtCH As Integer = 8
Dim retval As New List(Of UInt32)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToUInt32(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Function toU64(ByVal Shex As String) As List(Of UInt64)
Const cvtCH As Integer = 16
Dim retval As New List(Of UInt64)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToUInt64(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'unsigned 32 bit max = FFFFFFFF
'unsigned 64 bit max = FFFFFFFF
'signed 32 bit max = 7FFFFFFF
'signed 64 bit max = 7FFFFFFF
Dim hexS As String = "A14152464C203230304232323020572F544947455234352E"
Dim hexS2 As String = "A14152464C203230304232323020572F54494745523435"
toByte(hexS)
toU32(hexS)
toU64(hexS)
End Sub
#6
1
Dim hex As String
hex = "A14152464C203230304232323020572F544947455234352E"
Dim dec As Long
Dim hexpart As String
For x As Integer = 1 To (hex.Length / 2)
hexpart = hex.Substring((x * 2) - 2, 2)
dec = Int32.Parse(hexpart, System.Globalization.NumberStyles.HexNumber)
Debug.Print("Hex = " + hex + ",HexPart = " + hexpart + ", Dec = " + dec.ToString + Environment.NewLine)
Next
This won't work for Decimal and the Hex is too long for integer... but you get the idea. You could split it and recombine.
这对于Decimal不起作用,Hex对整数来说太长了......但是你明白了。你可以将它拆分并重新组合。
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = A1, Dec = 161
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 41, Dec = 65
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 52, Dec = 82
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 46, Dec = 70
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 4C, Dec = 76
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 20, Dec = 32
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 42, Dec = 66
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 20, Dec = 32
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 57, Dec = 87
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 2F, Dec = 47
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 54, Dec = 84
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 49, Dec = 73
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 47, Dec = 71
#7
1
Write one yourself.
自己写一个。
You will need to tokenize the string, then start from the right, and work your way left.
您将需要对字符串进行标记,然后从右侧开始,然后继续前进。
int weight = 1;
While Looping
{
If (token(i) == "F") { DecimalValue += 15 * weight; }
If (token(i) == "E") { DecimalValue += 14 * weight; }
If (token(i) == "D") { DecimalValue += 13 * weight; }
If (token(i) == "C") { DecimalValue += 12 * weight; }
If (token(i) == "B") { DecimalValue += 11 * weight; }
If (token(i) == "A") { DecimalValue += 10 * weight; }
else { DecimalValue += token(i) * weight; }
weight = weight * 16;
}
Something like that.
这样的事情。
#8
1
Tried the others replies and this was the one that worked for me:
试过其他人回复,这是对我有用的:
Convert.ToInt32(yourHEXString, 16)
#9
0
Dim hex As String = "A1B2C3D4"
Dim int As Integer = Val("&H" & hex)