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 th
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
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.
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.
Dim Value As Integer = Val("&H" & YourHexadecimalStringHere)