急求:delphi中16进制转成10进制的函数

cwf6521783 2005-03-23 10:46:24
急求:delphi中16进制转成10进制的函数
以前用:
DecNum:=StrToInt('$'+HexNum);
但是不行,比如16进制数:83231d27,本来应该是:2200116519
但是上面转出的是负数
...全文
2269 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
happywww 2005-03-24
  • 打赏
  • 举报
回复
var
DecNum: Cardinal;
huyongxiao 2005-03-23
  • 打赏
  • 举报
回复
{十六进制转换为十进制}
function 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;
oushengfen 2005-03-23
  • 打赏
  • 举报
回复
二进制用byte类型啊,8位
十六进制用Word、DWord、Integer都行,Word是2个字节的,Dword和integer都是4个字节的
赋值时直接用十六进制值赋值,给你几个函数,对你应该有用

楼主作什么的?交流一下: amiao_107@163.com

{输入一个字符串;输出每个字符的HEX码的一个串}
function StrToHex(const S: string): string;
var
i,ilen: Integer;
wTemp : Word;
begin
Result := '';
if S = '' then Exit;

ilen := Length(S);

for i:=1 to ilen do
begin
wTemp := Ord(S[i]);
Result := Result + IntToStr(wTemp);
end;
end;

{二进制转换为十六进制}
function BinToHex(const aBin: string): string;
var
I,L,K: Integer;
begin
Result := '';
if aBin = '' then Exit;

K := 0;
L := Length(aBin);
for I:=1 to L do
K := K + Trunc(StrToInt(aBin[I]) * Power(2, L-I));

Result := '$' + IntToHex(K, 4);
end;

{二进制转换为十进制}
function BinToInt(const aBin: string): Integer;
var
I,L,K: Integer;
begin
Result := 0;
if aBin = '' then Exit;

K := 0;
L := Length(aBin);
for I:=1 to L do
K := K + Trunc(StrToInt(aBin[I]) * Power(2, L-I));

Result := K;
end;

{十六进制转换为二进制}
function HexToBin(const aHex: string): string;
var
S: string;
I,K: Integer;
begin
Result := '';
if aHex = '' then Exit;

S := ''; Result := '';
K := StrToInt('$'+aHex);
while K>=2 do
begin
S := S + IntToStr(K mod 2);
K := K div 2;
end;
S := S + IntToStr(K);
K := Length(S);
if K<4 then
S := S + Copy('000', 1, 4-K);
K := Length(S);
for I:=K downto 1 do
Result := Result + S[I];
end;

{十六进制转换为十进制}
function 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;

{十进制转换为二进制}
function IntToBin(const aInt: Integer): string;
var
s: string;
i,j: Integer;
begin
s := ''; Result := '';
i := aInt;
while i >= 2 do
begin
s := s + IntToStr(i mod 2);
i := i div 2;
end;
s := s + IntToStr(i);
i := Length(s);
if i < 4 then
s := s + Copy('000', 1, 4-i);
i := Length(s);
for j:=i downto 1 do
Result := Result + s[j];
end;
oushengfen 2005-03-23
  • 打赏
  • 举报
回复
{十六进制转换为十进制}
function 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;
何鲁青 2005-03-23
  • 打赏
  • 举报
回复
十六进制的数值太大了...用int64试试.
ufo20020427 2005-03-23
  • 打赏
  • 举报
回复
var DecNum:int64;
begin
memo1.clear;
DecNum:=StrToInt64('$'+'83231d27');
memo1.text:=inttostr(DecNum);
end;
xzhifei 2005-03-23
  • 打赏
  • 举报
回复
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[i]))* involution(length(hexstr)-i);
Result:=temp;
end;

ly_liuyang 2005-03-23
  • 打赏
  • 举报
回复
数值太大的话,就要用自己写的过程

http://lysoft.7u7.net

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧