Mik*_*ley 3 installer pascal inno-setup freepascal
我想将String转换为字节数组,代码如下所示:
procedure StringToByteArray(const s : String; var tmp: array of Byte);
var
i : integer;
begin
For i:=1 to Length(s) do
begin
tmp[i-1] := Ord(s[i]);
end;
end;
Run Code Online (Sandbox Code Playgroud)
s [i]这里是第i个String元素(= pos i处的char),我将其数值保存到tmp.
这适用于某些角色,但并非适用于所有角色,例如:
Ord('•')返回Dec(149),这是我所期望的.
但在我的程序中,Ord(s [i])返回相同角色的Dec(8226)!
Edit1:我认为缺陷在于我的其他功能"ByteArrayToStr"
转换时......
tmp:= 149 // tmp is of type byte
Log('experiment: ' + Chr(tmp)); // prints "•"
Log('experiment2 ' + IntToStr(Ord(Chr(tmp)))); // prints 149
Run Code Online (Sandbox Code Playgroud)
......来回,这似乎有效.
但是在以下函数中使用相同的转换将不会这样做:
function ByteArrayToStr( a : array of Byte ) : String;
var
S:String;
I:integer;
begin
S:='';
For I:=0 to Length(a) -1 do
begin
tmp := Chr(a[I]) ; // for a[I] equals 149 this will get me "?" instead of "•"
S:=S+tmp;
end;
Result:=S;
end;
Run Code Online (Sandbox Code Playgroud)
为清楚起见: ByteArrayToStr没有按预期将Ord(149)转换为"•",因此StringToByteArray以后不会工作
您需要将参数转换为AnsiString类型.通过这样做,您可以编写如下函数:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
[Code]
procedure StringToByteArray(const S: AnsiString; out ByteArray: array of Byte);
var
I: Integer;
begin
SetArrayLength(ByteArray, Length(S));
for I := 1 to Length(S) do
ByteArray[I - 1] := Ord(S[I]);
end;
function ByteArrayToString(const ByteArray: array of Byte): AnsiString;
var
I: Integer;
begin
SetLength(Result, GetArrayLength(ByteArray));
for I := 1 to GetArrayLength(ByteArray) do
Result[I] := Chr(ByteArray[I - 1]);
end;
procedure InitializeWizard;
var
S: AnsiString;
ByteArray: array of Byte;
begin
S := '•';
StringToByteArray(S, ByteArray);
MsgBox(IntToStr(ByteArray[0]), mbInformation, MB_OK);
S := '';
S := ByteArrayToString(ByteArray);
MsgBox(S, mbInformation, MB_OK);
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1530 次 |
| 最近记录: |