进制转换



{输入一个字符串;输出每个字符的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 := '此处换成美元符号,TMD,直接用发表出来就乱了!' + 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('此处换成美元符号,TMD,直接用发表出来就乱了!'+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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值