Friday, July 11, 2014

autoit chinese encoding gbk gb2312 clipboard

Sometimes AutoIT has encode problem. For example, for the stock trade software, after open and Send("^c")
ÓªÒµ²¿¹«¸æ is returned.

Below auotit script will return the correct display even on English OS
$sCodePage=54936 ;936 GB2312
$sData = ClipGet()
MsgBox(4096, "String() representation" , _EncodingToUnicode_API($sData,$sCodePage))


Func _EncodingToUnicode_API($sString,$sCodePage_Identifier)
Local $BufferSize = StringLen($sString) * 2
Local $Buffer = DllStructCreate("byte[" & $BufferSize & "]")

Local $Return = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", _
"int", $sCodePage_Identifier, _
"int", 0, _
"str", $sString, _
"int", StringLen($sString), _
"ptr", DllStructGetPtr($Buffer), _
"int", $BufferSize)

Local $UnicodeBinary = DllStructGetData($Buffer, 1)
Local $UnicodeHex1 = StringReplace($UnicodeBinary, "0x", "")
Local $StrLen = StringLen($UnicodeHex1)
Local $UnicodeString, $UnicodeHex2, $UnicodeHex3

For $i = 1 To $StrLen Step 4
$UnicodeHex2 = StringMid($UnicodeHex1, $i, 4)
$UnicodeHex3 = StringMid($UnicodeHex2, 3, 2) & StringMid($UnicodeHex2, 1, 2)
$UnicodeString &= ChrW(Dec($UnicodeHex3))
Next
$Buffer = 0
Return $UnicodeString
EndFunc

No comments:

Post a Comment