麻辣家族討論版版's Archiver

laigs218 發表於 2021-7-9 09:13

vb 讀取unicode

網路上找到 vb 讀取unicode 的方法

Dim textBytes() As Byte, headBytes(2) As Byte
   fn = App.Path & "\test.txt"
If Dir(fn) = "" Then
MsgBox "文件不存在,請先按一下【寫入】按鈕生成“MyUnicode.txt”文件。", vbInformation
   Exit Sub
  End If
   Open fn For Binary As #1
       Get #1, , headBytes()  '讀取文件頭。
    Close #1
  If headBytes(0) = 255 And headBytes(1) = 254 Then '是Unicode編碼。十六進位為:FF 、FE。
     Open fn For Binary As #1
     ReDim textBytes(LOF(1) - 2) '減去文件頭佔用的2個位元組。
     Get #1, 3, textBytes()  '第三個位元組起為文本內容。
     Close #1
    Text1.Text = textBytes()  '在 VB 中字串是 UniCode 格式,所以Unicode碼直接賦值即可顯示文本內容。
     RichTextBox1.Text = textBytes()
Else
        MsgBox "非Unicode編碼,不予讀入,請按一下【寫入】按鈕。", vbInformation
End If

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供