首页 > 代码库 > 十六进制数转为字符串

十六进制数转为字符串

//-----------------------------------------------
//16进制字符转整数,16进制字符与字符串转换中间函数
//-----------------------------------------------
function HexToInt2(hex: AnsiString): integer;
var
  i: integer;
  function Ncf(num, f: integer): integer;
  var
    i: integer;
  begin
    Result := 1;
    if f = 0 then exit;
    for i := 1 to f do
      result := result * num;
  end;
  function HexCharToInt(HexToken: Ansichar): integer;
  begin
    if Ord(HexToken) > 97 then
      HexToken := Ansichar(Chr(Ord(HexToken) - 32));
      Result := 0;
    if (Ord(HexToken) > 47) and (Ord(HexToken) < 58) then { chars 0....9 }
      Result := Ord(HexToken) - 48
    else if (Ord(HexToken) > 64) and (Ord(HexToken) < 71) then { chars A....F }
      Result := Ord(HexToken) - 55;
  end;
var
  a16int:Integer;
  ancf:integer;
begin
  result := 0;
    hex := ansiuppercase(trim(hex));
  if hex = ‘‘ then
    exit;
  for i := 1 to length(hex) do
  // result := result + HexCharToInt(hex[i]) * ncf(16, length(hex) - i);
  begin
    a16int := HexCharToInt(hex[i]);
    ancf := ncf(16, length(hex) - i);
    result := result + a16int * ancf;
   end;
end;

//-----------------------------------------------
//16进制字符转字符串
//-----------------------------------------------

function hextostring(str: AnsiString): AnsiString;
var
  s:AnsiString;
  i,j:integer;
  p:pchar;
  t:AnsiString;
  a16int:integer;
  a16char:AnsiString;
begin
   s:=‘‘;
   i:=1;
   //26044  236 ;182
   while i< length(str) do begin
      t:= str[i] + str[i+1];
      a16int := hextoint2(t);  //
//    a16int := 26044;
//    a16int := hextoint(‘ECB6‘);//
//    a16char := chr(a16int);
      if a16int <> 0 then
      begin
        a16char := ANSICHAR(a16int);
        s:= s + a16char;
      end;
//      s := #$EC + #$B6;
//      s := #236 + #182;
//      s := chr(236) + chr(182);
      i:=i+2;
   end;
   result:=s;
end;

本文出自 “一切有为法,如梦幻泡影” 博客,请务必保留此出处http://kaixinbuliao.blog.51cto.com/2567365/1871829

十六进制数转为字符串