转换一个二进制变量为一个字符串.
BinaryToString ( 表达式 [, 标志] )
表达式 | 一个需要进行转换的二进制变量. |
标志 | [可选参数] 修改数据编码转换格式: 标志 = 1 (默认), 二进制 数据原先为 ANSI 编码 标志 = 2, 二进制 数据原先为 UTF16 小编码 标志 = 3, 二进制 数据原先为 UTF16 大编码 标志 = 4, 二进制 数据原先为 UTF8 编码 |
成功: | 从一个二进制变量中转换得到的字符串. |
失败: | 空字符串. @error 将会被设为下列值: |
1 - 输入字符串长度为0. | |
2 - 输入的字符串只有奇数的对数,但是它被假定为 UTF16 编码. (必须包含偶数字节的变量于 UTF16). |
Example()
Func Example()
; Define the string that will be converted later.
; NOTE: This string may show up as ?? in the help file and even in some editors.
; This example is saved as UTF-8 with BOM. It should display correctly in editors
; which support changing code pages based on BOMs.
Local Const $sString = "Hello - 你好"
; Temporary variables used to store conversion results. $sBinary will hold
; the original string in binary form and $sConverted will hold the result
; afte it's been transformed back to the original format.
Local $sBinary, $sConverted
; Convert the original UTF-8 string to an ANSI compatible binary string.
$sBinary = StringToBinary($sString)
; Convert the ANSI compatible binary string back into a string.
$sConverted = BinaryToString($sBinary)
; Display the resulsts. Note that the last two characters will appear
; as ?? since they cannot be represented in ANSI.
DisplayResults($sString, $sBinary, $sConverted, "ANSI")
; Convert the original UTF-8 string to an UTF16-LE binary string.
$sBinary = StringToBinary($sString, 2)
; Convert the UTF16-LE binary string back into a string.
$sConverted = BinaryToString($sBinary, 2)
; Display the resulsts.
DisplayResults($sString, $sBinary, $sConverted, "UTF16-LE")
; Convert the original UTF-8 string to an UTF16-BE binary string.
$sBinary = StringToBinary($sString, 3)
; Convert the UTF16-BE binary string back into a string.
$sConverted = BinaryToString($sBinary, 3)
; Display the resulsts.
DisplayResults($sString, $sBinary, $sConverted, "UTF16-BE")
; Convert the original UTF-8 string to an UTF-8 binary string.
$sBinary = StringToBinary($sString, 4)
; Convert the UTF8 binary string back into a string.
$sConverted = BinaryToString($sBinary, 4)
; Display the resulsts.
DisplayResults($sString, $sBinary, $sConverted, "UTF8")
EndFunc ;==>Example
; Helper function which formats the message for display. It takes the following parameters:
; $sOriginal - The original string before conversions.
; $sBinary - The original string after it has been converted to binary.
; $sConverted- The string after it has been converted to binary and then back to a string.
; $sConversionType - A human friendly name for the encoding type used for the conversion.
Func DisplayResults($sOriginal, $sBinary, $sConverted, $sConversionType)
MsgBox(4096, "", "Original:" & @CRLF & $sOriginal & @CRLF & @CRLF & "Binary:" & @CRLF & $sBinary & @CRLF & @CRLF & $sConversionType & ":" & @CRLF & $sConverted)
EndFunc ;==>DisplayResults