delphi如何将16进制转成10进制(20分)

  • 主题发起人 主题发起人 zz770813
  • 开始时间 开始时间
Z

zz770813

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将串口下的16进制转成10进制.如下代码
procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+inttohex(rbuf,2)+'';
viewstring:='接受'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');

end;
 
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer



Call HexToBin to convert the hexadecimal string Text to the binary value it represents.

Text is a string representation of a hexadecimal value.

Buffer returns the resulting value in binary.

BufSize is the size of Buffer. Text needs to point to at least 2*BufSize hexadecimal characters, because each two hexadecimal characters represent one byte.

HexToBin returns the number of characters in Buffer that have not been used because Text did not contain valid hexadecimal characters ('0'..'f').

Note: The hexadecimal number must use lower-case characters; HexToBind does not recognize upper-case characters.
 
我一般是用insert($十六进制)
 
一般十六进制是0xxx的形式,你只要传进后面两位就可以了!
function int HexToInt(String Hex)
var
int HighHex;
int LowHex;
begin
HighHex := Int(Char(Hex[0]));
LowHex := Int(Cahr(Hex[1]));
Result := HighHex * 16 + LowHex;
end;
 
自已可以编写一个函数来实现,利用查表方式得到一位十六进制转成十进制,然后,每从右到左的位依次乘上其权值,即可准确得到十进制,方法实际是按权展开。
 
function ReceiveHexToInt(s: string): Integer; //把接收的HEX转为Int
var
I: Integer;
s1: string;
begin
s1 := '';
for I := 1 to Length(s) do
begin
s1 := s1 + FormatFloat('00', Ord(s));
end;
Result := StrToInt(s1);
end;
 
function HexCharToInt(HexToken : char):Integer;//十六进制字符转十进制
begin
if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }

Result:=0;

if (HexToken>#47) and (HexToken<#58) then { chars 0....9 }
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then { chars A....F }
Result:=Ord(HexToken)-65 + 10;
end;

function involution(exp:integer):integer; //乘方 指数大于1
var
a:integer;
begin
Result:=1;
if exp<=0 then
exit;
for a:=1 to exp do
Result:=Result*16;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit2.Text:=inttostr(involution(strtoint(edit1.Text)));
end;

function HexstrToInt(hexstr : string):Integer; //十六进制字符串转十进制
var
i,temp:integer;

begin
temp:=0;

For i:=1 To length(hexstr) do
temp:=temp+(HexCharToInt(hexstr))* involution(length(hexstr)-i);
Result:=temp;
end;
 
嘿嘿,上面说的很清楚了
 
如何只是16进制转为10进制很简单的
只需要在16进制前加个$,然后进行Int64ToStr
再来一次StrtoInt就搞定了。。。
 
不说了。
为什么我会的别人总比我手脚快呢?^_^
 
后退
顶部