Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Just for an example
Inside the Textbox.Text I have:

String = "01-23-45-67-89"

I want to convert it to Byte array with same value with &H
Byte = " &H01, &H23, &H45, &H67, &H89"

any suggestion please.

What I have tried:

Private Function HexStringToBytes(ByVal input As String) As Byte()
    Dim byteStrings() As String = input.Split(New Char() {","c})
    If (byteStrings.Length > 0) Then
        Dim retVal() As Byte = CType(Array.CreateInstance(GetType(Byte), byteStrings.Length), Byte())
        Dim idx As Integer = 0
        For Each byteString As String In byteStrings
            retVal(idx) = Convert.ToByte(byteString, 16)
            idx += 1
        Return retVal
    End If
    Return New Byte() {}
End Function
Updated 23-Mar-21 23:09pm
jsc42 11-Mar-21 5:16am    
Are you trying to create a string with "&Hxx" texts in it, as per your description; or an array of bytes as per your sample code?
If it is the string, then you can do a simple text conversion e.g. Dim byteString As String = "&H" & inputString.Replace("-", ", &H")
If it is the array of bytes, then I'll let someone else reply as the technique is slightly harder and will need something in a 'Solution' section rather that a 'Comment' section.

There is a function in .NET which might help: SoapHexBinary.Parse(String) Method (System.Runtime.Remoting.Metadata.W3cXsd2001) | Microsoft Docs[^] - it requires spaces instead of hyphens, but string.Replace will deal with that.
Dim s As String = "FF FF FF FF 82 00 01 02 03 04 00 05 01 02 03 04 05 ee ed"
Dim bytes As Byte() = SoapHexBinary.Parse(s).Value
Share this answer
Same as yesterday[^], but with a different increment:
Shared Function HexStringToBytes(ByVal hexString As String) As Byte()
    Dim result As New List(Of Byte)()
    For i As Integer = 0 To hexString.Length - 1 Step 3
        result.Add(Convert.ToByte(hexString.Substring(i, 2), 16))
    Return result.ToArray()
End Function
Or, using a variation of your function:
Shared Function HexStringToBytes(ByVal input As String) As Byte()
    Dim byteStrings() As String = input.Split(New Char() {"-"c})
    Dim retVal() As Byte = Array.ConvertAll(byteStrings, Function(s) Convert.ToByte(s, 16))
    Return retVal
End Function

EDIT: Based on your comment, you are actually trying to convert an array of bytes to a string. Again, I gave you that function yesterday:
Shared Function BytesToHexString(ByVal bytes As Byte()) As String
    Dim sb As New StringBuilder()
    For i As Integer = 0 To bytes.Length - 1
    Return sb.ToString()
End Function
Share this answer
Nicky Ralph 11-Mar-21 6:47am    
I tried to use the code above and get the value of textbox1 to show it on Richtext1, I got and error " dimensional array of Byte cannot be converted to string"
Private Sub Button1_Click(
        'RichTextBox1.Text = HexStringToBytes(TextBox1.Text)
End Sub
Richard Deeming 11-Mar-21 6:51am    
Well, once again, you've described things the wrong way round.

Your question is about converting a string to an array of bytes.

That error tells me you are trying to convert an array of bytes to a string.

Stop randomly typing code, and start thinking about what you are trying to do!
Richard Deeming 11-Mar-21 7:04am    
The textbox text is a STRING. It is NOT a byte array!

You want to convert a byte array to a string. As I showed you yesterday.
Nicky Ralph 11-Mar-21 7:08am    
YES, I want to convert string into byte array with same value of hex. something like this, Inside the textbox1.text is a HEX VALUE

TEXTBOX1.TEXT = "A1-B2-C3-D4-E5"

I want to convert it into byte like

byte() ={&HA1, &HB2, &HC3, &HD4, &hE5}
Richard Deeming 11-Mar-21 7:09am    
No, you don't.

You want to convert the string to a byte array - which I've already shown you how to do - and then convert the byte array back to a string - which I've also shown you how to do.

You cannot store a byte array in a string property!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900