원본 : http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2143325&SiteID=1
구글을 검색하다가 발견한 vb utf-8변환소스
vb에서 웹으로 post 데이터를 넘길시 인코딩이 맞지 않아 문제가 되던 것을 이 소스로 해결했다. 해결하는데 무려 3일이란 시간이....
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
'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
'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
'프로그래밍' 카테고리의 다른 글
WebConfigurationManager의 사용 (0) | 2015.02.13 |
---|---|
WebRequest 사용하여 Post로 보내기 (0) | 2015.02.13 |
Refactoring (0) | 2015.02.13 |
xsl에 파라미터를 넘기는 방법 (0) | 2015.02.13 |
구성된 식별자가 올바르지 않아 서버 프로세스를 시작하지 못했습니다. (0) | 2015.02.13 |