VB 멀티바이트 형식을 유니코드 형식으로 바꿔주는 함수
페이지 정보
작성자
본문
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
Private Declare Function GetACP Lib "Kernel32" () As Long
Private Const CP_ACP = 0 ' default to ANSI code page
Private Const CP_UTF8 = 65001 ' default to UTF-8 code page
'--------------------------------
' AToW
'
' ANSI to UNICODE conversion, via a given codepage.
'--------------------------------
Public Function AToW(ByVal st As String, Optional ByVal cpg As Long = -1, Optional ByVal lFlags As Long = 0) As String
Dim stBuffer As String
Dim cwch As Long
Dim pwz As Long
Dim pwzBuffer As Long
If cpg = -1 Then cpg = GetACP()
pwz = StrPtr(st)
cwch = MultiByteToWideChar(cpg, lFlags, pwz, -1, 0&, 0&)
stBuffer = String$(cwch + 1, vbNullChar)
pwzBuffer = StrPtr(stBuffer)
cwch = MultiByteToWideChar(cpg, lFlags, pwz, -1, pwzBuffer, Len(stBuffer))
AToW = Left$(stBuffer, cwch - 1)
End Function
'--------------------------------
' WToA
'
' UNICODE to ANSI conversion, via a given codepage
'--------------------------------
Public Function WToA(ByVal st As String, Optional ByVal cpg As Long = -1, Optional ByVal 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
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
Private Declare Function GetACP Lib "Kernel32" () As Long
Private Const CP_ACP = 0 ' default to ANSI code page
Private Const CP_UTF8 = 65001 ' default to UTF-8 code page
'--------------------------------
' AToW
'
' ANSI to UNICODE conversion, via a given codepage.
'--------------------------------
Public Function AToW(ByVal st As String, Optional ByVal cpg As Long = -1, Optional ByVal lFlags As Long = 0) As String
Dim stBuffer As String
Dim cwch As Long
Dim pwz As Long
Dim pwzBuffer As Long
If cpg = -1 Then cpg = GetACP()
pwz = StrPtr(st)
cwch = MultiByteToWideChar(cpg, lFlags, pwz, -1, 0&, 0&)
stBuffer = String$(cwch + 1, vbNullChar)
pwzBuffer = StrPtr(stBuffer)
cwch = MultiByteToWideChar(cpg, lFlags, pwz, -1, pwzBuffer, Len(stBuffer))
AToW = Left$(stBuffer, cwch - 1)
End Function
'--------------------------------
' WToA
'
' UNICODE to ANSI conversion, via a given codepage
'--------------------------------
Public Function WToA(ByVal st As String, Optional ByVal cpg As Long = -1, Optional ByVal 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
'Use this to encode before you send...
Function EncodeUTF8(ByVal cnvUni As String)
If cnvUni = vbNullString Then Exit Function
EncodeUTF8 = WToA(cnvUni, CP_UTF8, 0)
End Function
Function EncodeUTF8(ByVal cnvUni As String)
If cnvUni = vbNullString Then Exit Function
EncodeUTF8 = WToA(cnvUni, CP_UTF8, 0)
End Function
'Use this to decode received strings...
Function DecodeUTF8(ByVal cnvUni As String)
If cnvUni = vbNullString Then Exit Function
cnvUni = WToA(cnvUni, CP_ACP)
DecodeUTF8 = AToW(cnvUni, CP_UTF8)
End Function
Function DecodeUTF8(ByVal cnvUni As String)
If cnvUni = vbNullString Then Exit Function
cnvUni = WToA(cnvUni, CP_ACP)
DecodeUTF8 = AToW(cnvUni, CP_UTF8)
End Function
댓글목록
등록된 댓글이 없습니다.