Sign in to follow this  
Followers 0
gto250

The question of ansi2UTF8

1 post in this topic

#1 ·  Posted (edited)

I used the function of _ConvertAnsiToUtf8

convert GB2312 to utf8,but it have a problem

I used the result of _ConvertAnsiToUtf8($sText)

Then tcpsend()

but in the server it's Missing the character (The server use UTF8)

Func _ConvertAnsiToUtf8($sText)
    Local $tUnicode = _WinAPI_MultiByteToWideChar($sText)
    If @error Then Return SetError(@error, 0, "")
    Local $sUtf8 =_WinAPI_WideCharToMultiByte(DllStructGetPtr($tUnicode), 65001)
    If @error Then Return SetError(@error, 0, "")
    Return SetError(0, 0, $sUtf8)
EndFunc   ;==>_ConvertAnsiToUtf8

Func _WinAPI_MultiByteToWideChar($sText, $iCodePage = 0, $iFlags = 0, $bRetString = False)
    Local $sTextType = "ptr", $pText = $sText

    If IsDllStruct($sText) Then
        $pText = DllStructGetPtr($sText)
    Else
        If Not IsPtr($sText) Then $sTextType = "STR"
    EndIf

    ; compute size for the output WideChar
    Local $aResult = DllCall("kernel32.dll", "int", "MultiByteToWideChar", "uint", $iCodePage, "dword", $iFlags, $sTextType, $pText, _
            "int", -1, "ptr", 0, "int", 0)
    If @error Then Return SetError(@error, @extended, 0)

    ; allocate space for output WideChar
    Local $iOut = $aResult[0]
    Local $tOut = DllStructCreate("wchar[" & $iOut & "]")
    Local $pOut = DllStructGetPtr($tOut)

    $aResult = DllCall("kernel32.dll", "int", "MultiByteToWideChar", "uint", $iCodePage, "dword", $iFlags, $sTextType, $pText, _
            "int", -1, "ptr", $pOut, "int", $iOut)
    If @error Then Return SetError(@error, @extended, 0)

    If $bRetString Then Return DllStructGetData($tOut,1)
    Return $tOut
EndFunc   ;==>_WinAPI_MultiByteToWideChar
Func _WinAPI_WideCharToMultiByte($pUnicode, $iCodePage = 0, $bRetString = True)
    Local $sUnicodeType = "ptr"
    If IsDllStruct($pUnicode) Then
        $pUnicode = DllStructGetPtr($pUnicode)
    Else
        If Not IsPtr($pUnicode) Then $sUnicodeType = "wstr"
    EndIf
    Local $aResult = DllCall("kernel32.dll", "int", "WideCharToMultiByte", "uint", $iCodePage, "dword", 0, $sUnicodeType, $pUnicode, "int", -1, _
            "ptr", 0, "int", 0, "ptr", 0, "ptr", 0)
    If @error Then Return SetError(@error, @extended, "")

    Local $tMultiByte = DllStructCreate("char[" & $aResult[0]& "]")
    Local $pMultiByte = DllStructGetPtr($tMultiByte)

    $aResult = DllCall("kernel32.dll", "int", "WideCharToMultiByte", "uint", $iCodePage, "dword", 0, $sUnicodeType, $pUnicode, "int", -1, _
            "ptr", $pMultiByte, "int", $aResult[0], "ptr", 0, "ptr", 0)
    If @error Then Return SetError(@error, @extended, "")

    If $bRetString Then Return DllStructGetData($tMultiByte,1)
    Return $tMultiByte
EndFunc   ;==>_WinAPI_WideCharToMultiByte
Edited by gto250

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0