СообЧа > База Знаний > Программирование > Visual Basic

Вопрос

Как перекодировать WIN1251 в UTF-8?

Ответ

Код формы:

sCodePage=CP_UTF8

Private Function EncodeUTF8(ByVal cnvUni As String) As String
     If cnvUni = vbNullString Then Exit Function
     EncodeUTF8 = StrConv(WToA(cnvUni, sCodePage, 0), vbUnicode)
End Function

Код модуля:

Public Function WToA(ByVal st As String, Optional ByVal cpg As Long = -1, Optional
lFlags As Long = 0) As String
     Dim stBuffer As String
     Dim cwch As Long
     Dim pwz As Long
     Dim pwzBuffer As Long
     Dim lpUsedDefaultChar As Long
     
     If cpg = -1 Then cpg = GetACP()
     pwz = StrPtr(st)
     cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, 0&, 0&, ByVal 0&, ByVal 0&)
     stBuffer = String$(cwch + 1, vbNullChar)
     pwzBuffer = StrPtr(stBuffer)
     cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, pwzBuffer, Len(stBuffer), ByVal
0&, ByVal 0&)
     WToA = Left$(stBuffer, cwch — 1)
End Function

[Алексей]



Copyright © 2000-2004 Сообщество Чайников
Контактная информация