Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String to List of Bytes

Tags:

vb.net

This has to be incredibly simple, but I must not be looking in the right place.

I'm receiving this string via a FTDI usb connection:

'UUU'

I would like to receive this as a byte array of

[85,85,85]

In Python, this I would convert a string to a byte array like this: [ord(c) for c in 'UUU']

I've looked around, but haven't figured this out. How do I do this in Visual Basic?

like image 389
David Rinck Avatar asked Dec 04 '22 08:12

David Rinck


2 Answers

Use the Encoding class with the correct encoding.

C#:

// 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")
like image 132
Oded Avatar answered Jan 30 '23 16:01

Oded


depends on what kind of encoding you want to use but for UTF8 this works, you could chane it to UTF16 if needed.

Dim strText As String = "UUU"
Dim encText As New System.Text.UTF8Encoding()
Dim btText() As Byte
btText = encText.GetBytes(strText)
like image 37
Mertis Avatar answered Jan 30 '23 17:01

Mertis