ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Convert string from one codepage to another (https://www.excelbanter.com/excel-programming/389269-convert-string-one-codepage-another.html)

Petr Bazant[_2_]

Convert string from one codepage to another
 
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ÚČTOVÁNÍ ČÁSTI
SOUD.POPLATKU".


NickHK

Convert string from one codepage to another
 
Petr,
StrConv maybe .

NickHK

"Petr Bazant" wrote in message
ups.com...
Can someone please give me an advise, how to convert in VBA e.g. this
string "VYTOVN 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 "VYCTOVN CSTI
SOUD.POPLATKU".



Petr Bazant[_2_]

Convert string from one codepage to another
 
On May 14, 4:45 am, "NickHK" wrote:
Petr,
StrConv maybe .

NickHK

"Petr Bazant" wrote in message

ups.com...
Can someone please give me an advise, how to convert in VBA e.g. this
string "VYTOVN 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 "VYCTOVN CSTI
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 = "VYTOVN STI SOUD.POPLATKU"
out = StrConvCP1ToCP2(src, 852, 1250)
End Sub





All times are GMT +1. The time now is 05:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com