Delphi에서 HexToStr 함수와 StrToHex 함수를 실현하다

5733 단어 Delphi
function TransChar(AChar: Char): Integer;
begin
  if AChar in ['0'..'9'] then
  Result := Ord(AChar) - Ord('0')
  else
  Result := 10 + Ord(AChar) - Ord('A');
end;
function StrToHex(AStr: string): string;
var
  I ,Len: Integer;
  s:char;
begin
  len:=length(AStr);
  Result:='';
  for i:=1 to len  do
  begin
    s:=AStr[i];
    Result:=Result +' '+IntToHex(Ord(s),2); //       16     ,
                                            //
  end;
  Delete(Result,1,1); //           
end;
function HexToStr(AStr: string): string;
var
  I,len : Integer;
  CharValue: Word;
  Tmp:string;
  s:char;
begin
  Tmp:='';
  len:=length(Astr);
  for i:=1 to len  do
  begin
    s:=Astr[i];
    if s <> ' ' then Tmp:=Tmp+ string(s);
  end;
  Result := '';
  For I := 1 to Trunc(Length(Tmp)/2) do
  begin
    Result := Result + ' ';
    CharValue := TransChar(Tmp[2*I-1])*16 + TransChar(Tmp[2*I]);
    if (charvalue < 32) or (charvalue > 126)  then Result[I] := '.'   //       
    else Result[I] := Char(CharValue);
  end;
end;

좋은 웹페이지 즐겨찾기