|
|
2005/9/4 下午 01:18:48
我用inet.urlopen把websit的原始檔(編碼是UTF-8)下載到txt File. 再用函數把這txt file有用的資料input到form的textbox. 但是中文字亂碼.. 請問怎樣把UTF-8 轉 BIG5 碼? 多謝各位幫忙...
|
|
|
|
2005/9/5 下午 03:10:56
找到方法了嗎? 可以分享一下嘛?我也需要這個咧..UTF -> BIG5 ....VB6 >我用inet.urlopen把websit的原始檔(編碼是UTF-8)下載到txt File. >再用函數把這txt file有用的資料input到form的textbox. >但是中文字亂碼.. >請問怎樣把UTF-8 轉 BIG5 碼? >多謝各位幫忙...
|
|
|
|
2005/9/5 下午 04:24:34
試看看 MultiByteToWideChar API函式
|
|
|
|
2005/9/5 下午 04:36:32
有範例嗎?... >試看看 MultiByteToWideChar API函式
|
|
|
|
2005/9/5 下午 04:38:48
MultiByteToWideChar API函式 此函式ㄛ試不成功,所有utf-8轉big5都試過了,都不行用... 如果成功請告知........加油!
|
|
|
2005/9/5 下午 07:03:11
我一直認為這應該是很簡單的東西說... @@
Unicode對UTF-8的對應關係如下: UCS-4 range(hex) UTF-8 byte sequence(binary) 00000000 - 0000007F 0xxxxxxx 00000080 - 000007FF 110xxxxx 10xxxxxx 00000800 - 0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
寫成程式就是:
Function UTF8ToUnicode(utf8() As Byte) As String Dim i, length, ch, def_ch As Long Dim b1, b2, b3 As Byte Dim s As String s = "" def_ch = Asc("?") length = UBound(utf8) i = 1 While i <= length b1 = utf8(i) i = i + 1 If b1 < 128 Then ' 1 byte: 00-7F ch = b1 ElseIf 192 <= b1 And b1 <= 128 Then ' 2 byte: 080-7FF If i > length Then ch = def_ch Else b2 = utf8(i) i = i + 1 If 128 <= b2 And b2 <= 191 Then ch = (b1 - 192) * 64 + (b2 - 128) Else ch = def_ch End If End If ElseIf 224 <= b1 And b1 <= 239 Then ' 3 byte: 0800-FFFF If i + 1 > length Then ch = def_ch i = i + 1 Else b2 = utf8(i) b3 = utf8(i + 1) i = i + 2 If 128 <= b2 And b2 <= 191 And 128 <= b3 And b3 <= 191 Then ch = (b1 - 224) * 4096 + (b2 - 128) * 64 + (b3 - 128) Else ch = def_ch End If End If Else ch = def_ch End If s = s & ChrW(ch) Wend UTF8ToUnicode = s End Function
我對vb的語法不很熟, 要用的人請自行把它調到最快的處理方式.
另外, 如果讀取UTF8的文字檔, 請記得去掉最前面的EF, BB, BF 3個檔頭字元 (16進位是239,187,191).
|
|
|
|
2005/9/5 下午 07:11:33
> ElseIf 192 <= b1 And b1 <= 128 Then '' 2 byte: 080-7FF
這行寫錯了, 應該192 <= b1 and b1 <= 223, 雖然對中文不影響, 但會影響到歐洲/中東語系.
|
|
|
2005/9/5 下午 10:42:42
Private Const CP_UTF8 = 65001
Private Declare Function MultiByteToWideChar Lib "kernel32" ( _ ByVal CodePage As Long, ByVal dwFlags As Long, _ ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _ ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Sub Command1_Click() On Error GoTo ErrHandler Dim Big5str As String Dim tmpUTF8() As Byte Dim lLength As Long,lBytes As Long, lWC As Long '123.txt UTF8編碼的文字檔 ReDim tmpUTF8(FileLen("D:\123.txt") - 1) Open "D:\123.txt" For Binary As #1 Get #1, , tmpUTF8 Close #1
lBytes = UBound(tmpUTF8) - LBound(tmpUTF8) + 1 lWC = lBytes Big5str = String$(lBytes, Chr(0)) lLength = MultiByteToWideChar(CP_UTF8, 0, VarPtr(tmpUTF8(0)), lBytes, _ StrPtr(Big5str), lWC) Big5str = Left$(Big5str, lLength) Text1.Text = Big5str
Exit Sub ErrHandler: MsgBox Err.Description End Sub
如有錯請指正.................
|
|
|
|
2005/9/5 下午 11:06:44
非常多謝兩位.......
|
|
|
|
2005/9/5 下午 11:49:27
那 BIG5 轉 UTF-8 又該如何 不解 請賜教
|
|
|
|
2005/9/6 上午 12:03:34
可以試看看 WideCharToMultiByte API函式........
|
|
|
|
2005/9/6 下午 03:19:04
以下是BIG5轉UTF-8,但轉出的結果有些錯,小弟也不曉得該如何改進。 有哪位先進可以幫個忙嗎...@@a
Private Const CP_UTF8 = 65001
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Public Function UTF8_Encode(ByVal Text As String) As String
Dim sBuffer As String Dim lLength As Long
If Text <> "" Then lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(Text), -1, 0, 0, 0, 0) sBuffer = Space$(lLength) lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(Text), -1, StrPtr(sBuffer), Len(sBuffer), 0, 0) sBuffer = StrConv(sBuffer, vbUnicode) UTF8_Encode = Left$(sBuffer, lLength - 1) Else UTF8_Encode = "" End If
End Function
|
|
|
|
2005/9/6 下午 10:28:12
Private Const CP_ACP = 0 Private Const CP_UTF8 = 65001
Private Declare Function MultiByteToWideChar Lib "kernel32" ( _ ByVal CodePage As Long, ByVal dwFlags As Long, _ ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _ ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" ( _ ByVal CodePage As Long, ByVal dwFlags As Long, _ ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, _ ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _ ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
Private Sub Command1_Click() On Error GoTo ErrHandler Dim Big5str As String,UTF8str as String Dim tmpBig5() as Byte,tmpUTF8() As Byte Dim lLength As Long,lBytes As Long, lWC As Long
ReDim tmpBig5(FileLen("D:\big5.txt") - 1) Open "D:\big5.txt" For Binary As #1 Get #1, , tmpBig5 Close #1
lBytes = UBound(tmpBig5) - LBound(tmpBig5) + 1 lWC = lBytes Big5str = String$(lBytes, Chr(0)) Call MultiByteToWideChar(CP_ACP, 0, VarPtr(tmpBig5(0)), lBytes, _ StrPtr(Big5str), lWC) lWC = Len(Big5str) lBytes = 2 * lWC ReDim tmpUTF8(0 To lBytes - 1) lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(Big5str), lWC, _ VarPtr(tmpUTF8(0)), lBytes, vbNullString, 0) ReDim Preserve tmpUTF8(0 To lLength - 1) UTF8str = StrConv(tmpUTF8, vbUnicode) Text1.Text = UTF8str
Exit Sub ErrHandler: MsgBox Err.Description End Sub
如有錯誤請指正~~
|
|
|
|
2005/9/7 下午 12:33:36
ichirolu大 你好 你的Code小弟剛才試了一下,轉出來有錯誤的情形。 像我在big5.txt裡加入「測試中.....」,轉出來是錯的。
Option Explicit
Private Const CP_ACP = 0 Private Const CP_UTF8 = 65001
Private Declare Function MultiByteToWideChar Lib "kernel32" ( _ ByVal CodePage As Long, ByVal dwFlags As Long, _ ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _ ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" ( _ ByVal CodePage As Long, ByVal dwFlags As Long, _ ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, _ ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _ ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
Private Sub Form_Load() On Error GoTo ErrHandler Dim Big5str As String, UTF8str As String Dim tmpBig5() As Byte, tmpUTF8() As Byte Dim lLength As Long, lBytes As Long, lWC As Long
ReDim tmpBig5(FileLen("D:\big5.txt") - 1) Open "D:\big5.txt" For Binary As #1 Get #1, , tmpBig5 Close #1
lBytes = UBound(tmpBig5) - LBound(tmpBig5) + 1 lWC = lBytes Big5str = String$(lBytes, Chr(0)) Call MultiByteToWideChar(CP_ACP, 0, VarPtr(tmpBig5(0)), lBytes, StrPtr(Big5str), lWC) lWC = Len(Big5str) lBytes = 2 * lWC ReDim tmpUTF8(0 To lBytes - 1) lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(Big5str), lWC, VarPtr(tmpUTF8(0)), lBytes, vbNullString, 0) ReDim Preserve tmpUTF8(0 To lLength - 1) UTF8str = StrConv(tmpUTF8, vbUnicode) Open "d:\UTF-8.txt" For Binary As #1 Put #1, 1, &HEF Put #1, 2, &HBB Put #1, 3, &HBF Put #1, 4, UTF8str Close #1 Unload Me Exit Sub ErrHandler: MsgBox Err.Description End Sub
|
|
|
|
2005/9/7 下午 02:38:23
UTF8的字串最好用BYTE ARRAY, 不要用String來表示, 不然輸出入常會有問題.
UTF8str = StrConv(tmpUTF8, vbUnicode)
這一行拿掉, 直接使用tmpUTF8, 應該就會正確了.
|
|
|
|
2005/9/7 下午 03:44:16
@@"..真的可以了 感謝 ichirolu大 提供的Code還有青衫大哥的觀念指正。
非常感謝你們 :D
|
|
|
|
2005/9/7 下午 03:55:50
不用客氣,多多嘗試,一定會試出結果的~~~~
|
|
|
|
2005/12/13 下午 07:47:33
utf-8轉big-5我需要但不會轉!我用inet.urlopen把websit的原始檔(編碼是UTF-8)下載到 str1字串,如何將str1字串utf-8轉big-5,謝謝!因為我不太會,謝謝指導一下!
|
|
|
|
|
|
|
|
|
|
|
| ■ |
| ■ |
| ■ |
| ■ |
| ■ |
|
| ■ |
| ■ |
| ■ |
| ■ |
|
| ■ |
|
| ■ |
| ■ |
| ■ |
| ■ |
|
|
|
板主 :
小樓
Top 10 評價排行 |
 |
Visual Basic 6.0/VBA |
|
|
|
|
|
|
|
|
| Visual Basic 6.0/VBA |
 |
|
| |
專家等級 |
評價 |
|
| |
一代宗師 |
10000 |
|
| |
曠世奇才 |
5000 |
|
| |
頂尖高手 |
3000 |
|
| |
卓越專家 |
1500 |
|
| |
優秀好手 |
750
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Microsoft Internet Explorer
6.0. Screen 1024x768 pixel. High Color (16 bit).
2000-2010 程式設計俱樂部 http://www.programmer-club.com.tw/ |
|
|