Convert string to byte list

It should be incredibly simple, but I should not look in the right place.

I get this line through the FTDI USB connection:

'UUU' 

I would like to get this as an array of bytes

 [85,85,85] 

In Python, I would convert the string to an array of bytes as follows: [ord (c) for c to "UUU"]

I looked around, but did not understand this. How to do it in Visual Basic?

+4
source share
2 answers

depends on what encoding you want to use, but for UTF8 it works, you can port it to UTF16 if necessary.

 Dim strText As String = "UUU" Dim encText As New System.Text.UTF8Encoding() Dim btText() As Byte btText = encText.GetBytes(strText) 
+8
source

Use the Encoding class with the correct encoding.

WITH#:

 // Assuming string is UTF8 Encoding utf8 = Encoding.UTF8Encoding(); byte[] bytes = utf8.GetBytes("UUU"); 

VB.NET:

 Dim utf8 As Encoding = Encoding.UTF8Encoding() Dim bytes As Byte() = utf8.GetBytes("UUU") 
+9
source

All Articles