各位高手,小弟初学delphi,有一些16进制的问题不太明白,望各位高手帮忙解决下,谢谢//16进制转10进制函数
function TForm1.HexToInt(const aHex: string): Integer;
var
I,L,K: Integer;
begin
Result := 0;
if aHex = '' then Exit;
K := 0;
L := Length(aHex);
for I:=1 to L do
begin
if (not(aHex[I] in['A'..'F'])) and (not(aHex[I] in['a'..'f'])) then
K := K + Trunc(StrToInt(aHex[I]) * power(16, L-I))
else case aHex[I] of
'a', 'A' : K := K + Trunc(10 * Power(16, L-I));
'b', 'B' : K := K + Trunc(11 * Power(16, L-I));
'c', 'C' : K := K + Trunc(12 * Power(16, L-I));
'd', 'D' : K := K + Trunc(13 * Power(16, L-I));
'e', 'E' : K := K + Trunc(14 * Power(16, L-I));
'f', 'F' : K := K + Trunc(15 * Power(16, L-I));
end;
end;
Result := K;
end;procedure TForm1.Button1Click(Sender: TObject);
var
aa:string;
bb:integer;
cc:byte;
begin
aa:=Edit1.text;//edit1里面输入0f;
bb:=HexToInt(aa);
showmessage(inttostr(bb));end; // 输出结果为 15 换成这样
procedure TForm1.Button1Click(Sender: TObject);
var bb:integer;
cc:byte;
begin
cc:=$0f;
bb:=HexToInt(inttostr(cc));
showmessage(inttostr(bb));end; //输出结果为21
请问这是什么问题,而后面这种是我想要的 利用一个byte类型变量来存放一个16进制数,然后再转换成10进制,用一个integer类型来存放,谢谢指点,谢谢!
function TForm1.HexToInt(const aHex: string): Integer;
var
I,L,K: Integer;
begin
Result := 0;
if aHex = '' then Exit;
K := 0;
L := Length(aHex);
for I:=1 to L do
begin
if (not(aHex[I] in['A'..'F'])) and (not(aHex[I] in['a'..'f'])) then
K := K + Trunc(StrToInt(aHex[I]) * power(16, L-I))
else case aHex[I] of
'a', 'A' : K := K + Trunc(10 * Power(16, L-I));
'b', 'B' : K := K + Trunc(11 * Power(16, L-I));
'c', 'C' : K := K + Trunc(12 * Power(16, L-I));
'd', 'D' : K := K + Trunc(13 * Power(16, L-I));
'e', 'E' : K := K + Trunc(14 * Power(16, L-I));
'f', 'F' : K := K + Trunc(15 * Power(16, L-I));
end;
end;
Result := K;
end;procedure TForm1.Button1Click(Sender: TObject);
var
aa:string;
bb:integer;
cc:byte;
begin
aa:=Edit1.text;//edit1里面输入0f;
bb:=HexToInt(aa);
showmessage(inttostr(bb));end; // 输出结果为 15 换成这样
procedure TForm1.Button1Click(Sender: TObject);
var bb:integer;
cc:byte;
begin
cc:=$0f;
bb:=HexToInt(inttostr(cc));
showmessage(inttostr(bb));end; //输出结果为21
请问这是什么问题,而后面这种是我想要的 利用一个byte类型变量来存放一个16进制数,然后再转换成10进制,用一个integer类型来存放,谢谢指点,谢谢!
我定义 aa:=$0f; 通过网络传输后,接收端在edit或者在dbgird
bb:integer; bb:=strtoint(inttostr(aa)); 那么16进制转10进制就这样可以了吗?
我试了一下 得到的结果确实是15
S1 : Byte;
ss : String; S1 := $F;
FmtStr(ss,'%.*x',[2,s1]);
ShowMessage(ss);