4

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?

0

2 Answers 2

9

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")
Sign up to request clarification or add additional context in comments.

Comments

8

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)

Comments

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.