还有一个,代码长一些。
function EnCode1(text:String): String;
var
  i, j, len, cur :Integer;
  s :String;
begin
  Result := '';
  len := Length(text);
  i := 1;
  j := 0;
  while (i<=len) do
  begin
    if i < len then
      cur := (ord(text[i]) shr j) or (ord(text[i+1]) shl (7-j)) and $FF
    else
      cur := (ord(text[i]) shr j) and $7F;
    FmtStr(s, '%2.2X', [cur]);
    Result := Result + s;
    inc(i);
    j := (j+1) mod 7;
    if j = 0 then inc(i);
  end;
end;{EnCode1}

解决方案 »

  1.   

    //但是我感到很奇怪,你的这种编码是Unicode吗?function DisCode2(text:String): String;
    var
      i, len:Integer;
    begin
      Result := '';
      i := 1;
      len := Length(text);
      while (i<=len) do
      begin
        Result := Result + Chr(StrToIntDef('$' + Copy(text, i, 4), 0));
        inc(i, 4);
      end;
    end; {DisCode2}
      

  2.   

    多谢zswang(伴水) 。
    是Unicode。我参照CP936写的,代码页和操作系统相关。
    能看看EnCode1对应的DisCode1吗?
      

  3.   

    zswang(伴水),还没上班?
    帮忙看看DisCode1。谢谢。
      

  4.   

    //不存在可逆性的编码怎么解码?
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Edit1.Text := EnCode1(#$7F) + ',' + EnCode1(#$FF);
    end;
      

  5.   

    function DisCode1(text:String): String;
    var
      i, j, len, a, b :Integer;
    begin
      Result := '';
      len := Length(text);
      i := 1;
      j := 0;
      while (i<=len) do
      begin
        if i > 2 then
          a := StrToIntDef('$' + Copy(text, i - 2, 2), 0)
        else a := 0;
        b := StrToIntDef('$' + Copy(text, i, 2), 0);
        Result := Result + Chr((a shr (8 - j)) or (b shl j) and $7F);
        Inc(i, 2);
        j := (j + 1) mod 7;
        if j = 0 then Result := Result + Chr((b shr 1) and $7F);
      end;
    end;{DisCode1}
      

  6.   

    function DisCode1(text:String): String;
    var
      i, j, len, a, b :Integer;
    begin
      Result := '';
      len := Length(text);
      i := 1;
      j := 0;
      while (i<=len) do
      begin
        if i > 2 then
          a := StrToIntDef('$' + Copy(text, i - 2, 2), 0)
        else a := 0;
        b := StrToIntDef('$' + Copy(text, i, 2), 0);
        Result := Result + Chr((a shr (8 - j)) or (b shl j) and $7F);
        Inc(i, 2);
        j := (j + 1) mod 7;
        if j = 0 then Result := Result + Chr((b shr 1) and $7F);
      end;
    end;{DisCode1}
      

  7.   

    function DisCode1(text:String): String;
    var
      i, j, len, a, b :Integer;
    begin
      Result := '';
      len := Length(text);
      i := 1;
      j := 0;
      while (i<=len) do
      begin
        if i > 2 then
          a := StrToIntDef('$' + Copy(text, i - 2, 2), 0)
        else a := 0;
        b := StrToIntDef('$' + Copy(text, i, 2), 0);
        Result := Result + Chr((a shr (8 - j)) or (b shl j) and $7F);
        Inc(i, 2);
        j := (j + 1) mod 7;
        if j = 0 then Result := Result + Chr((b shr 1) and $7F);
      end;
    end;{DisCode1}