How to convert a string with pairs of numbers to an array of bytes without converting in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text

Public Class Program
    Public Shared Sub PrintByteArray(ByVal bytes As Byte())
        Dim sb = New StringBuilder("byte[] = ")
        Dim b
 
        For Each b In bytes
            sb.Append(b & ", ")
        Next
 
        Console.WriteLine(sb.ToString())
    End Sub
    
    Public Shared Sub Main()
        Dim str As String = "97987099304825"
        Dim bytes((str.Length / 2) - 1) As Byte

        For i As Integer = 0 to str.Length - 1 Step 2
            bytes(i / 2) = Convert.ToByte(str.Substring(i, 2))
        Next i

        Console.WriteLine(Encoding.UTF8.GetString(bytes))
        
        PrintByteArray(bytes)

    End Sub
End Class




' run:
'
' abFc0
' byte[] = 97, 98, 70, 99, 30, 48, 25, 
'

 



answered May 28, 2024 by avibootz

Related questions

...