EZGPIB:将十进制字符串转换为十六进制(Delphi Scripted Pascal)
EZGPIB: Converting Decimal String to Hexadecimal (Delphi Scripted Pascal)
我想在 Delphi 脚本化的基于 Pascal 的程序 EZGPIB 中将十进制字符串(范围在 0 到 260 亿之间)转换为十六进制字符串。我似乎在 Internet 上找到了一段合适的代码,但在 EZGPIB 中无法正确实现它。也许精通 Pascal 的人可以帮助我让它工作? EZGPIB 在 google 上可用,无需安装即可使用。代码如下:
program hex_conv;
var
gwar:string;
function Hex(v: Longint; w: Integer): String;
var
s : String;
i : Integer;
hexc : array [0 .. 15] of Char;
begin
hexc[0] := '0';
hexc[1] := '1';
hexc[2] := '2';
hexc[3] := '3';
hexc[4] := '4';
hexc[5] := '5';
hexc[6] := '6';
hexc[7] := '7';
hexc[8] := '8';
hexc[9] := '9';
hexc[10] := 'A';
hexc[11] := 'B';
hexc[12] := 'C';
hexc[13] := 'D';
hexc[14] := 'E';
hexc[15] := 'F';
s[0] := Chr(w);
for i := w downto 1 do begin
s[i] := hexc[v and $F];
v := v shr 4
end;
Result := s;
end;
begin
EZGPIB_ScreenClear;
gwar:=hex(15,1);
EZGPIB_ScreenWriteLn(gwar);
gwar:=hex(1,1);
EZGPIB_ScreenWriteLn(gwar);
end.
如果我尝试编译它,EZGPIB 编译器会抛出一个运行时错误:"s[0] := Chr(w);" 处超出字符串范围并适用于 "gwar:=hex(15,1);" 的最大输入。
这是一个相当老式的实现,它使用了关于字符串内部工作的知识,而这在现代 Pascal 中不再有效。 s[0] 引用了 'length byte' 并有效地固定了字符串的长度。 SetLength( s, w) 将是现代的等价物。
我想在 Delphi 脚本化的基于 Pascal 的程序 EZGPIB 中将十进制字符串(范围在 0 到 260 亿之间)转换为十六进制字符串。我似乎在 Internet 上找到了一段合适的代码,但在 EZGPIB 中无法正确实现它。也许精通 Pascal 的人可以帮助我让它工作? EZGPIB 在 google 上可用,无需安装即可使用。代码如下:
program hex_conv;
var
gwar:string;
function Hex(v: Longint; w: Integer): String;
var
s : String;
i : Integer;
hexc : array [0 .. 15] of Char;
begin
hexc[0] := '0';
hexc[1] := '1';
hexc[2] := '2';
hexc[3] := '3';
hexc[4] := '4';
hexc[5] := '5';
hexc[6] := '6';
hexc[7] := '7';
hexc[8] := '8';
hexc[9] := '9';
hexc[10] := 'A';
hexc[11] := 'B';
hexc[12] := 'C';
hexc[13] := 'D';
hexc[14] := 'E';
hexc[15] := 'F';
s[0] := Chr(w);
for i := w downto 1 do begin
s[i] := hexc[v and $F];
v := v shr 4
end;
Result := s;
end;
begin
EZGPIB_ScreenClear;
gwar:=hex(15,1);
EZGPIB_ScreenWriteLn(gwar);
gwar:=hex(1,1);
EZGPIB_ScreenWriteLn(gwar);
end.
如果我尝试编译它,EZGPIB 编译器会抛出一个运行时错误:"s[0] := Chr(w);" 处超出字符串范围并适用于 "gwar:=hex(15,1);" 的最大输入。
这是一个相当老式的实现,它使用了关于字符串内部工作的知识,而这在现代 Pascal 中不再有效。 s[0] 引用了 'length byte' 并有效地固定了字符串的长度。 SetLength( s, w) 将是现代的等价物。