function _StrToHex(const s : string) : string; var iCount : integer; Value, CH, CL : Byte; begin SetLength(Result, 2 * Length(s)); for iCount:= 1 to Length(s) do begin Value:= Byte(S[iCount]); CH:= (Value shr 4) and $0F; CL:= Value and $0F; if CL < $0A then CL:= CL + $30 else CL:= CL + $37; if CH < $0A then CH:= CH + $30 else CH:= CH + $37; Result[iCount * 2 - 1]:= Char(CH); Result[iCount * 2]:= Char(CL); end; end;//与 _strtohex 配合使用 function _HexStrToStr(const HexStr : string) : string; const NumericBin : array[$30..$39] of Byte = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9); CharBin : array[$41..$46] of Byte = (10, 11, 12, 13, 14, 15); var BH, BL : Byte; iCount : integer; P : ^Byte; begin SetLength(Result, Length(HexStr) div 2); P:= Pointer(Result); for iCount:= 0 to Length(HexStr) div 2 - 1 do begin BH:= Byte(HexStr[iCount * 2 + 1]); if BH >= $41 then BH:= CharBin[BH] else BH:= NumericBin[BH]; BL:= Byte(HexStr[iCount * 2 + 2]); if BL >= $41 then BL:= CharBin[BL] else BL:= NumericBin[BL]; BH:= BH shl 4 + BL and $0F; P^:= BH; Inc(P); end; end;
ShowMessage(format('%x', [100]));
我想知道是该怎么表示16进制的数。例如c中是用0x表示的,delphi中怎么表示?
var i: Integer;i := $FF;
那我要是定义的byte型那?是不是用bytetohex?
例 procedure TForm1.Button1Click(Sender: TObject);var i: Integer; begin Label1.Caption := ''; for i := 1 to Length(Edit1.Text) do begin try Label1.Caption := Label1.Caption + IntToHex(Edit1.Text[i],2) + ' '; except Beep; end; end; end;
上面的有些问题 var i: Integer; begin Label1.Caption := ''; for i := 1 to Length(Edit1.Text) do begin try Label1.Caption := Label1.Caption + IntToHex(ord(Edit1.Text[i]),2) + ' '; except Beep; end; end; end;
var
iCount : integer;
Value, CH, CL : Byte;
begin
SetLength(Result, 2 * Length(s));
for iCount:= 1 to Length(s) do
begin
Value:= Byte(S[iCount]);
CH:= (Value shr 4) and $0F;
CL:= Value and $0F;
if CL < $0A then
CL:= CL + $30
else
CL:= CL + $37;
if CH < $0A then
CH:= CH + $30
else
CH:= CH + $37;
Result[iCount * 2 - 1]:= Char(CH);
Result[iCount * 2]:= Char(CL);
end;
end;//与 _strtohex 配合使用
function _HexStrToStr(const HexStr : string) : string;
const NumericBin : array[$30..$39] of Byte = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
CharBin : array[$41..$46] of Byte = (10, 11, 12, 13, 14, 15);
var
BH, BL : Byte;
iCount : integer;
P : ^Byte;
begin
SetLength(Result, Length(HexStr) div 2);
P:= Pointer(Result);
for iCount:= 0 to Length(HexStr) div 2 - 1 do
begin
BH:= Byte(HexStr[iCount * 2 + 1]);
if BH >= $41 then
BH:= CharBin[BH]
else
BH:= NumericBin[BH];
BL:= Byte(HexStr[iCount * 2 + 2]);
if BL >= $41 then
BL:= CharBin[BL]
else
BL:= NumericBin[BL];
BH:= BH shl 4 + BL and $0F;
P^:= BH;
Inc(P);
end;
end;
i: Integer;i := $FF;
procedure TForm1.Button1Click(Sender: TObject);var
i: Integer;
begin
Label1.Caption := '';
for i := 1 to Length(Edit1.Text) do
begin
try
Label1.Caption := Label1.Caption + IntToHex(Edit1.Text[i],2) + ' ';
except
Beep;
end;
end;
end;
var
i: Integer;
begin
Label1.Caption := '';
for i := 1 to Length(Edit1.Text) do
begin
try
Label1.Caption := Label1.Caption + IntToHex(ord(Edit1.Text[i]),2) + ' ';
except
Beep;
end;
end;
end;
inttohex很好用的,建议使用。
显示时要把它写到字符串中。
要显示成十六进制:
1:Label1.caption := IntToHex(IntVal);
2:Label1.Caption := Format('%X',[IntVal]);