如:16进ASCII码:35H 44H 转换成 5DH
                33H 31H 转换成 31H
                30H 30H 转换成 00H

解决方案 »

  1.   

    先将16进制ASCII码转换成通常的字符串,然后使用strtoint转换成数字,再将这些十进制数字转换成16进制
      

  2.   

    function TForm1.Convert(sSource: String): String;
    Const
      MinBase = 2;
      MaxBase = 36;  function StrToNum (const s: string; base: Integer;
                         neg: Boolean; max: Integer): Integer;
      var
        negate, done: Boolean;
        i, len, digit, mmb: Integer;
        c: Char;
        mdb, res: Integer;
      begin
        res:= 0;  i:= 1;  digit:= 0;
        if (base >= MinBase) and (base <= MaxBase) then
        begin
          mmb:= max mod base;
          mdb:= max div base;
          len:= Length (s);
          negate:= False;
          while (i <= len) and (s[i] = ' ') do Inc (i);
          if neg then
          begin
            case s[i] of
              '+': Inc (i);
              '-': begin  Inc (i);  negate:= TRUE; end;
            end; (* CASE *)
          end; (* IF neg *)
          done:= len > i;
          while (i <= len) and done do
          begin
            c:= Upcase (s[i]);
            case c of
              '0'..'9': digit:= ORD(c) - 48;
              'A'..'Z': digit:= ORD(c) - 55;
              else
                done:= FALSE
            end; (* CASE *)
            done:= done and (digit < base);
            if done then
            begin
              done:= (res < mdb) or ((res = mdb) and (digit <= mmb));
              IF done then
              begin
                res:= res * base + digit;
                Inc (i);
              end; (* IF done *)
            end; (* IF done *)
          end; (* WHILE *)
          if negate then res:= - res;
        end; (* IF done *)
        Result:= res;
      end;
    var
      iPos,iTemp:Integer;
      sTemp:String;
    begin
      Result:='';
      sSource:=Trim(sSource);
      While sSource>'' do
      begin
        iPos:=Pos('H',sSource);
        if iPos<=0 then Break;    sTemp:=Trim(Copy(sSource,1,iPos-1));
        sSource:=Trim(Copy(sSource,iPos+1,Length(sSource)));
        iTemp:=StrToNum(sTemp,16,false,MaxInt);
        Result:=Result+Chr(iTemp);
      end;
      Result:=Result+'H';
    end;