Convert string from one codepage to another

  • Thread starter Thread starter Petr Bazant
  • Start date Start date
P

Petr Bazant

Can someone please give me an advise, how to convert in VBA e.g. this
string "VYé¬TOVµNÖ ¬µSTI SOUD.POPLATKU" whichcomes from DOS 852
codepage to UTF-8 or to Windows-1250 codepage. I was trying to use
e.g. WideCharToMultiByte but I am not experienced enough to do it. BTW
the string should correctly look like this "VYÚČTOVÃNà ČÃSTI
SOUD.POPLATKU".
 
Petr,
StrConv maybe .

NickHK

Can someone please give me an advise, how to convert in VBA e.g. this
string "VYé¬TOVµNÖ ¬µSTI SOUD.POPLATKU" which comes from DOS 852
codepage to UTF-8 or to Windows-1250 codepage. I was trying to use
e.g. WideCharToMultiByte but I am not experienced enough to do it. BTW
the string should correctly look like this "VYÚCTOVÁNÍ CÁSTI
SOUD.POPLATKU".
 
Petr,
StrConv maybe .

NickHK


Can someone please give me an advise, how to convert in VBA e.g. this
string "VYé¬TOVµNÖ ¬µSTI SOUD.POPLATKU" which comes from DOS 852codepageto UTF-8 or to Windows-1250codepage. I was trying to use
e.g. WideCharToMultiByte but I am not experienced enough to do it. BTW
the string should correctly look like this "VYÚCTOVÁNÍ CÁSTI
SOUD.POPLATKU".

StrConv does not work directly. The solution (not from my head) is:

Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As String, ByVal cchWideChar As Long) As
Long
Private Declare Function WideCharToMultiByte Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
ByVal lpWideCharStr As String, ByVal cchWideChar As Long, _
ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, _
ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As
Long) As Long
Private Const MB_PRECOMPOSED = &H1
Private Const MY_CP_WINDOWS As Long = 1250
Private Const MY_CP_ISO_1 As Long = 28591
Private Const MY_CP_ISO_2 As Long = 28592 ' ISO Latin-2
Private Const MY_CP_IBM_852 As Long = 852


Private Function StrConvCP1ToCP2(sStr As String, lFromCP As Long, _
Optional lToCP As Long = 0) As String
Dim sStrW As String

sStrW = String$(2 * Len(sStr), vbNullChar)
MultiByteToWideChar lFromCP, MB_PRECOMPOSED, sStr, Len(sStr), _
sStrW, Len(sStr)
If lToCP = 0 Then
sStr = StrConv(sStrW, vbFromUnicode)
Else
WideCharToMultiByte lToCP, 0&, sStrW, Len(sStr), sStr,
Len(sStr), 0&, 0&
End If

StrConvCP1ToCP2 = sStr

End Function

Maybe someone will need it.

Sub test()
Dim out As String
Const src As String = "VYé¬TOVµNÖ ¬µSTI SOUD.POPLATKU"
out = StrConvCP1ToCP2(src, 852, 1250)
End Sub
 
Back
Top