0

How would I copy/convert a string containing an ascii representation of hex values in to a byte array containing the actual hex values? For example, I have a variable containing the hex values delimited by spaces (I can change the delimiter):

myString = "24 01 66 09 13 2F 1E 00 00 4E"

I want to put those values into a byte array:

Dim myByteArr(9) as Byte

1 Answer 1

2
Public Shared Function HexToBytes(ByVal s As String) As Byte()
    Dim bytes As String() = s.Split(" "c)
    Dim retval(bytes.Length - 1) As Byte
    For ix As Integer = 0 To bytes.Length - 1
        retval(ix) = Byte.Parse(bytes(ix), System.Globalization.NumberStyles.HexNumber)
    Next
    Return retval
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

You could further shorten this by using Array.ConvertAll, if desired

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.