码迷,mamicode.com
首页 > 其他好文 > 详细

十六进制数转为字符串

时间:2016-11-11 20:59:13      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:function   十六进制   result   字符串   

//-----------------------------------------------
//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

十六进制数转为字符串

标签:function   十六进制   result   字符串   

原文地址:http://kaixinbuliao.blog.51cto.com/2567365/1871829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!