2011-06-02 25 views
4

這必須非常簡單,但我不能在正確的位置查找。將字符串轉換爲字節列表

我接收經由FTDI USB連接字符串:

'UUU' 

我想收到此作爲

[85,85,85] 

一個字節數組在Python,這我將字符串轉換到一個像這樣的字節數組: [ord(c)for'in'UUU']

我環顧四周,但還沒有想通了。我如何在Visual Basic中執行此操作?

回答

7

取決於你想用什麼樣的編碼但UTF8這個工程,你可以在需要時向CHANE UTF16。

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

+1,因爲您需要它很差 – user774411 2011-06-02 20:32:34

+0

可憐的......還有一些。 – Oded 2011-06-02 20:38:26

9

使用具有正確編碼的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")