Jump to content
Sign in to follow this  
Ward

_StringToBinary, _BinaryToString (Support any Code Page)

Recommended Posts

Ward

StringTobinary() and BinaryToString() are the build-in functions, but they can't handle chinese string in ansi mode.

So I write my own version. This version not only support chinese string, but also support any codepage defined by Windows.

The $Flag in my version can be 1~4 (same as build-in functions), or Code Page Identifiers (link to MSDN), for exapmle, 65000=UTF7.

Here is a demo.

$Binary = _StringToBinary('English Test')
ConsoleWrite($Binary & @CRLF)
ConsoleWrite(_BinaryToString($Binary) & @CRLF)

$Binary = _StringToBinary('中文測試')
ConsoleWrite($Binary & @CRLF)
ConsoleWrite(_BinaryToString($Binary) & @CRLF)oÝ÷ Øë­¦ë[Êë"îWbû§rبÍ1ãë¾ëÞ÷ëÍ´ç¹ï~øx%ÈSzËtÄàçm4×}¹÷ÝNºÚnµ¼®²)à+ޮȨLxç¡:ï º÷½úóm9ã®{ß¾  b²Þ²Ý189@ÀùÛM5ßn}÷]¼ÛN7çôjëh×6Func _StringToBinary($String, $Flag = 1)
    If StringLen($String) = 0 Then Return ''
    If $Flag >= 2 And $Flag <= 4 Then Return StringToBinary($String, $Flag)

    Local $Ret = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", _
        "int", 0, _
        "int", 0, _
        "str", $String, _
        "int", -1, _
        "ptr", 0, _
        "int", 0)
    
    Local $Buffer = DllStructCreate("byte[" & ($Ret[0] * 2 - 2) & "]")

    $Ret = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", _
        "int", 0, _
        "int", 0, _
        "str", $String, _
        "int", -1, _
        "ptr", DllStructGetPtr($Buffer), _
        "int", $Ret[0] * 2 - 2)

    Local $UniString = DllStructGetData($Buffer, 1)
    Local $CodePage = $Flag

    If $Flag = 1 Then $CodePage = 0

    Local $UniStringLen = BinaryLen($UniString)
    Local $BufferLen = $UniStringLen * 2
    Local $Input = DllStructCreate("byte[" & $BufferLen & "]")
    Local $Output = DllStructCreate("byte[" & $BufferLen & "]")
    DllStructSetData($Input, 1, $UniString)
    Local $Ret = DllCall("kernel32.dll", "int", "WideCharToMultiByte", _
        "int", $CodePage, _
        "int", 0, _
        "ptr", DllStructGetPtr($Input), _
        "int", $UniStringLen / 2, _
        "ptr", DllStructGetPtr($Output), _
        "int", $BufferLen, _
        "int", 0, _
        "int", 0)

    $String = DllStructGetData($Output, 1)
    
    Return BinaryMid($String, 1, $Ret[0])
EndFunc

Func _BinaryToString($String, $Flag = 1)
    If $Flag >= 2 And $Flag <= 4 Then Return BinaryToString($String, $Flag)
    Local $CodePage = $Flag
    If $Flag = 1 Then $CodePage = 0

    Local $StrBuffer = DllStructCreate('byte[' & BinaryLen($String) + 1 & ']') ; Add 1 to make null-string
    DllStructSetData($StrBuffer, 1, $String)

    Local $Ret = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", _
        "int", $CodePage, _
        "int", 0, _
        "ptr", DllStructGetPtr($StrBuffer), _
        "int", -1, _
        "ptr", 0, _
        "int", 0)
    
    Local $Buffer = DllStructCreate("byte[" & ($Ret[0] * 2 - 2) & "]")
    $Ret = DllCall("Kernel32.dll", "int", "MultiByteToWideChar", _
        "int", $CodePage, _
        "int", 0, _
        "ptr", DllStructGetPtr($StrBuffer), _
        "int", -1, _
        "ptr", DllStructGetPtr($Buffer), _
        "int", $Ret[0] * 2 - 2)

    Local $UniString = DllStructGetData($Buffer, 1)

    Local $Input = DllStructCreate("byte[" & BinaryLen($UniString) & "]")
    DllStructSetData($Input, 1, $UniString)

    $Ret = DllCall("kernel32.dll", "int", "WideCharToMultiByte", _
        "int", 0, _
        "int", 0, _
        "ptr", DllStructGetPtr($Input), _
        "int", BinaryLen($UniString) / 2, _
        "int*", 0, _
        "int", 0, _
        "int", 0, _
        "int", 0)

    Local $OutputLen = $Ret[0]
    Local $Output = DllStructCreate("char[" & $OutputLen & "]")

    $Ret = DllCall("kernel32.dll", "int", "WideCharToMultiByte", _
        "int", 0, _
        "int", 0, _
        "ptr", DllStructGetPtr($Input), _
        "int", BinaryLen($UniString) / 2, _
        "ptr", DllStructGetPtr($Output), _
        "int", $OutputLen, _
        "int", 0, _
        "int", 0)

    Return DllStructGetData($Output, 1)
EndFunc

新版 _ArrayAdd 的白痴作者,不管是誰,去死一死好了

 

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  

×