Function Reference


_WinAPI_WideCharToMultiByte

Converts a UTF-16 (wide character) string to a multibyte string

#include <WinAPIConv.au3>
_WinAPI_WideCharToMultiByte ( $vUnicode [, $iCodePage = 0 [, $bRetNoStruct = True [, $bRetBinary = False]]] )

Parameters

$vUnicode String, DllStruct or Pointer to a byte array structure containing Unicode text to be converted
$iCodePage [optional] Code page to use in performing the conversion:
    0 - The current system Windows ANSI code page
    1 - The current system OEM code page
    2 - The current system Macintosh code page
    3 - The Windows ANSI code page for the current thread
    42 - Symbol code page
    65000 - UTF-7
    65001 - UTF-8
$bRetNoStruct [optional] Flags that indicate whether to return a String/Binary or a DllStruct (default True : String/Binary)
$bRetBinary [optional] Flags that indicate whether to return a Binary String or a String (default False : String)

Return Value

Success: A string/binary or a DllStruct containing a multibyte string/binary.
Failure: Sets the @error flag to non-zero, call _WinAPI_GetLastError() to get extended error information.

Remarks

Maps a UTF-16 (wide character) string to a new character string.
The new character string is not necessarily from a multibyte character set.

The $bRetBinary flag is designed for users of multi-byte codepages and forces the function to return a 0-terminated binary string or a struct containing it depending on $bRetNoStruct.

The $bRetNoStruct flag determines if the function should return only the string/binary or the entire struct.

Related

_WinAPI_MultiByteToWideChar

See Also

Search WideCharToMultiByte in MSDN Library.

Example

#include <WinAPIConv.au3>

;~ Global Const $CP = 0 ; CP_ACP default copepage
;~ Global Const $CP = GetACP() ; current codeoage
;~ Global Const $CP = 65001 ; UTF-8 certainly the best value
Global Const $CP = 932 ; CP_SHIFT_JIS

Local $sText = "データのダウンロードに失敗しました。"
;~ Local $sText = "abcdefg 1234567890"

Local $sOutput = @TAB & @TAB & "Copepage =" & $CP & @CRLF & @CRLF
$sOutput &= @TAB & "String[" & StringLen($sText) & "] = " & $sText & @CRLF & @CRLF

; ============== _WinAPI_WideCharToMultiByte Test ==============

Local $sTest = _WinAPI_WideCharToMultiByte($sText, $CP, True, False)
$sOutput &= "WideChar to String (MultiByte)" & @TAB & VarGetType($sTest) & " " & StringLen($sTest) & " :" & @CRLF & $sTest & @CRLF & @CRLF

$sTest = _WinAPI_WideCharToMultiByte($sText, $CP, True, True)
$sOutput &= "WideChar to Binary" & @TAB & VarGetType($sTest) & " " & BinaryLen($sTest) & " :" & @CRLF & $sTest & @CRLF & @CRLF

; ============== _WinAPI_MultiByteToWideChar Test ==============

Local $sMultiByte = _WinAPI_WideCharToMultiByte($sText, $CP, True, False)
$sOutput &= @CRLF & @TAB & "MultiByte[" & StringLen($sMultiByte) & "] = " & $sMultiByte & @CRLF & @CRLF

Local $tStruct = _WinAPI_MultiByteToWideChar($sMultiByte, $CP, 0, False)
$sOutput &= "MultiByte to Struct" & @TAB & @TAB & VarGetType($tStruct) & " " & DllStructGetSize($tStruct) & " :" & @CRLF & DllStructGetData($tStruct, 1) & @CRLF & @CRLF

$sTest = _WinAPI_MultiByteToWideChar($sMultiByte, $CP, 0, True)
$sOutput &= "MultiByte to String" & @TAB & VarGetType($sTest) & " " & StringLen($sTest) & " :" & @CRLF & $sTest & @CRLF & @CRLF

Local $iMB_TYPE = 0
If $sTest == $sText Then
        $sOutput &= @CRLF & @TAB & @TAB & "Conversion OK"
Else
        $sOutput &= @CRLF & @TAB & @TAB & " !!! Erreur de Conversion !!!"
        $iMB_TYPE = $MB_ICONERROR
EndIf

MsgBox($MB_SYSTEMMODAL + $iMB_TYPE, "Results", $sOutput)

Func GetACP()
        Local $aResult = DllCall("kernel32.dll", "int", "GetACP")
        If @error Or Not $aResult[0] Then Return SetError(@error + 20, @extended, "")
        Return $aResult[0]
EndFunc   ;==>GetACP