BinaryToString
转换二进制变量为字符串.
BinaryToString ( expression [, flag = 1] )
参数
expression
|
待转换的二进制变量
|
flag
|
[可选] 指定如何转换二进制数据:
$SB_ANSI (1) = 二进制数据为 ANSI 编码(默认)
$SB_UTF16LE (2) = 二进制数据为 UTF16 小编码
$SB_UTF16BE (3) = 二进制数据为 UTF16 大编码
$SB_UTF8 (4) = 二进制数据为 UTF8 编码
常量定义在 StringConstants.au3
|
返回值
成功:
|
返回二进制转换后的字符串.
|
失败:
|
返回空字符串. @error 设为非 0 值:
|
@error:
|
1 - 输入字符串的长度为 0.
2 - 字符串为奇数字节,但指定了 UTF16 编码 (必须偶数字节才是有效的 UTF16).
|
备注
String() 将二进制数据转换为十六进制数据, 本函数假定二进制数据为字符串值, 并将其适当转换.
详细说明参考 "Unicode 支持".
函数示例
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
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. $dBinary 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 $dBinary = Binary(""), $sConverted = ""
; Convert the original UTF-8 string to an ANSI compatible binary string.
$dBinary = StringToBinary($sString)
; Convert the ANSI compatible binary string back into a string.
$sConverted = BinaryToString($dBinary)
; Display the resulsts. Note that the last two characters will appear
; as ?? since they cannot be represented in ANSI.
DisplayResults($sString, $dBinary, $sConverted, "ANSI")
; Convert the original UTF-8 string to an UTF16-LE binary string.
$dBinary = StringToBinary($sString, $SB_UTF16LE)
; Convert the UTF16-LE binary string back into a string.
$sConverted = BinaryToString($dBinary, $SB_UTF16LE)
; Display the resulsts.
DisplayResults($sString, $dBinary, $sConverted, "UTF16-LE")
; Convert the original UTF-8 string to an UTF16-BE binary string.
$dBinary = StringToBinary($sString, $SB_UTF16BE)
; Convert the UTF16-BE binary string back into a string.
$sConverted = BinaryToString($dBinary, $SB_UTF16BE)
; Display the resulsts.
DisplayResults($sString, $dBinary, $sConverted, "UTF16-BE")
; Convert the original UTF-8 string to an UTF-8 binary string.
$dBinary = StringToBinary($sString, $SB_UTF8)
; Convert the UTF8 binary string back into a string.
$sConverted = BinaryToString($dBinary, $SB_UTF8)
; Display the resulsts.
DisplayResults($sString, $dBinary, $sConverted, "UTF8")
EndFunc ;==>Example
; Helper function which formats the message for display. It takes the following parameters:
; $sOriginal - The original string before conversions.
; $dBinary - 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, $dBinary, $sConverted, $sConversionType)
MsgBox($MB_SYSTEMMODAL, "", "Original:" & @CRLF & $sOriginal & @CRLF & @CRLF & "Binary:" & @CRLF & $dBinary & @CRLF & @CRLF & $sConversionType & ":" & @CRLF & $sConverted)
EndFunc ;==>DisplayResults
----------------------------------------
该函数可以通过命令 exect 调用
参见:
StringToBinary, Binary, String, IsBinary, StrinjToASCIIArray
exect=$var_h=FileOpen('%P%N',16)||$var_s=BinaryToString(FileRead($var_h),4)||FileClose($var_h)||_ViewValues('$var_s') ;; 以UTF-8编码读取光标下的文件数据的示例
exect=$var_b=StringToBinary('Hello!',1)||_ViewValues('$var_b')||$var_s=BinaryToString(Eval('var_b'),1)||_ViewValues('$var_s') ;; 将字符串转换为ANSI二进制类型和的例子
exect=$var_b=StringToBinary('Hello!',2)||_ViewValues('$var_b')||$var_s=BinaryToString(Eval('var_b'),2)||_ViewValues('$var_s') ;; 将字符串转换为二进制类型UTF-16 LE和返回的示例
exect=$var_b=StringToBinary('Hello!',3)||_ViewValues('$var_b')||$var_s=BinaryToString(Eval('var_b'),3)||_ViewValues('$var_s') ;; 将字符串转换为二进制类型UTF-16 BE和返回的示例
exect=$var_b=StringToBinary('Hello!',4)||_ViewValues('$var_b')||$var_s=BinaryToString(Eval('var_b'),4)||_ViewValues('$var_s') ;; 将字符串转换为二进制类型UTF-8和返回的示例
exect=$var_b=InetRead('http://tc-image.3dn.ru')||_ViewValues(BinaryToString('$var_b',4)) ;; 从页面tc-image.3dn.ru获取文本数据
© Аверин Андрей для Total Commander Image Averin-And@yandex.ru
|