16进制字节操作的几个函数

//取十六进制字符串第n字节值  字节之间有个空格
function AGetByteAt(HexValue:string; n:integer):string;
begin
  Result:=Copy(HexValue,(n-1)*3+1,2);
end;

 

//取十六进制字符串从第n字节开始的连续m个字节  字节之间有个空格
function AGetHexSubstr(HexValue:string; n, m : integer):string;
var
  i: Integer;
  temp: string;
begin
  temp:='';
  for i:=n to n+m-1 do
    temp:=temp+agetbyteat(HexValue,i);
  Result:=temp;
end;

 

//16进制字符串转换成字符串
function HexStrToStrA(S:string):string;
var
  t:Integer;
  ts:string;
  M,Code:Integer;
begin
  t:=1;
  Result:='';
  while t<=Length(S) do
  begin  
    while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
      inc(t);
    if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
      ts:='$'+S[t]
    else
      ts:='$'+S[t]+S[t+1];
    Val(ts,M,Code);
    if Code=0 then
      Result:=Result+Chr(M);
    inc(t,2);
  end;
end;

 

//字符串转换成16进制字符串
function StrToHexStrA(S:string):string;
var
  I:Integer;
begin
  for I:=1 to Length(S) do
  begin
    if I=1 then
      Result:=IntToHex(Ord(S[1]),2)
    else Result:=Result+' '+IntToHex(Ord(S[I]),2);
  end;
end;

 

//字节间加空格
function  AddSpaceSep(const s: string): string;
var
  i: Integer;
begin
  Result := '';
  for i := 1 to Length(s) do
  begin
    Result := Result + s[i];
    if i mod 2 = 0 then
      Result := Result + ' ';
  end;
end;

posted @ 2008-09-13 14:09  delphi中间件  阅读(237)  评论(0编辑  收藏  举报