开发者

VB.Net - Converting multi-byte hex values to decimal

How would I convert the two individual byte values (in hex) to a decimal value representing the concatenated hex values? For example, if I have the

Dim byte1 As Byte = &H99   
Dim byte2 As Byte = &H99

' I want the decimal representation of the hex value "9999" 
' (byte1.ToString + byte2.ToString) which should be "39321"

Currently I use the following code:

Dim decVal as integer
decVal = Val("&H" + Hex$(byte1).ToString + Hex$(byte2).开发者_StackOverflowToString)

However, when I use this, the value (decVal) comes out to "-26215"

What am I doing wrong in the conversion here?


Try the following:

decVal = byte1 * 256 + byte2

Your problem is when you call Val("&H9999"), if you use CInt("&H9999") or Integer.Parse("9999", System.Globalization.NumberStyles.HexNumber) you would get the correct answer (in this case at least)

If you look at the output from:

decVal = Val("&H9999")
Console.WriteLine(decVal.ToString("X"))

you get FFFF9999

I'm not sure why this is happening, but I would see this as a reason not to use the Val function to parse hexadecimal strings


@Patrick McDonald has a good inline way of doing it and if that's what you're looking for then I recommend using that. But I can't resist not giving you the overly complicated but cross-platform and expanded versions. One thing to note, you are asking to convert bytes in big-endian mode which is a very important topic to understand if you're playing with bytes.

Both functions below take a param array and perform conversions but in two different ways. One handles an arbitrarily large byte array (bound only by platform restrictions) and the other uses the built-in converter class that reads byte arrays.

Once again, let me stress the "overly complicated" part.

Private Shared Function ConvertBytes2(ByVal ParamArray bytes() As Byte) As UInt32
    ''//Reverse the array order
    Dim NewBytes = bytes.Reverse().ToList().ToArray()
    ''//Our return value
    Dim Dec As UInt32 = 0
    ''//Temporary value for bit shifting
    Dim T As UInt32
    ''//Loop through the bytes from left to right
    For I = (NewBytes.Count - 1) To 0 Step -1
        ''//Grab the byte
        T = NewBytes(I)
        ''//Shift it and add to our return value
        Dec += T << (8 * I)
    Next
    Return Dec
End Function
Private Shared Function ConvertBytes1(ByVal ParamArray bytes() As Byte) As UInt32
    ''//We want to read the bytes in big-endian order but BitConverter works in little-endian mode on most Windows systems (but not all) so convert if needed
    Dim NewBytes() As Byte
    If BitConverter.IsLittleEndian Then
        NewBytes = bytes.Reverse().ToList().ToArray()
    Else
        NewBytes = bytes
    End If
    ''//Our return value
    Dim Dec As UInt32
    ''//BitConverter can return UIn16, 32 or 64, we're only supporting 16 and 32 below
    If NewBytes.Count = 2 Then
        Dec = BitConverter.ToUInt16(NewBytes, 0)
    ElseIf NewBytes.Count = 4 Then
        Dec = BitConverter.ToUInt32(NewBytes, 0)
    Else
        ''//Invalid number of bytes sent
        Throw New ArgumentOutOfRangeException("bytes")
    End If
    Return Dec
End Function


    Dim Byte1 As Byte = &HA
    Dim Byte2 As Byte = &H99
    ''//&h0A99 = 2,713

    Trace.WriteLine(ConvertBytes1(Byte1, Byte2))
    Trace.WriteLine(ConvertBytes2(Byte1, Byte2))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜