int mir_decode(unsigned char *binary, char *text, int size)
{
unsigned char *step;
int loop, bits;
char idx; step = binary;
bits = 2; for (loop = 0; loop < size; ++loop)
{
idx = *text++ - 0x3c;
if (idx < 0 || idx >= 64)
{
return -1;
}
if (bits != 2)
{
step[-1] |= idx >> (8 - bits);
}
if (bits != 8)
{
*step++ = idx << bits;
bits += 2;
}
else
{
bits = 2;
}
} if (size & 3)
{
--step;
} return step - binary;
}int mir_encode(char *text, unsigned char *binary, int size)
{
char *step;
unsigned char b, idx;
int loop, bits; step = text;
b = 0;
bits = 6; for (loop = 0; loop < size; ++loop)
{
idx = *binary++;
*step++ = ((idx >> (8 - bits)) | b) + 0x3c;
b = (idx << (bits - 2)) & 0x3f;
bits -= 2;
if (!bits)
{
*step++ = b + 0x3c;
b = 0;
bits = 6;
}
}
if (bits != 6)
{
*step++ = b + 0x3c;
} return step - text;
}

解决方案 »

  1.   

    //int mir_decode(unsigned char *binary, char *text, int size)
    function mir_decode(binary,text:PChar; size:integer):integer;
    var
          // unsigned char *step;
            step:PChar;
           // int loop, bits;
            loop, bits:integer;
           // char idx;
           idx:Byte;
    begin step := binary;
    bits := 2;    // for (loop = 0; loop < size; ++loop)
          for loop:=0 to size-1 do
          begin
           //idx = *text++ - 0x3c;
             idx:=byte(text^)-$3c;
             inc(text);
             if (idx < 0) or( idx >= 64) then
             begin
               result:=-1;
               exit;
             end;
             if (bits <> 2) then
             begin
             // step[-1] |= idx >> (8 - bits);
                step[-1]:= Chr(Byte(step[-1]) or (idx shr (8 - bits)));
             end;
             if (bits <> 8) then
             begin
    {
    *step++ = idx << bits;
    bits += 2;
    }
                    step^:=Chr(idx shl bits);
                    inc(step);
                    inc(bits,2);
              end else
      bits := 2;      end;       if ((size and 3)>0) then
               dec(step);       result:=step - binary;
    end;//int mir_encode(char *text, unsigned char *binary, int size)
    function mir_encode(text,binary:PChar;size:integer):integer;
    var
         // unsigned char *step;
            step:PChar;
           // int loop, bits;
            loop, bits:integer;
           // unsigned char b, idx;
           b,idx:Byte;
    begin
    step := text;
    b := 0;
    bits:= 6;      // for (loop = 0; loop < size; ++loop)
           for loop:=0 to size-1 do
           begin
          // idx = *binary++;
                    idx:=Byte(binary^);
                    inc(binary);
          // *step++ = ((idx >> (8 - bits)) | b) + 0x3c;
                    step^:=Chr(((idx shr (8 - bits)) or b) + $3c)  ;
                    inc(step);
          // b = (idx << (bits - 2)) & 0x3f;
                    b:=(idx shl (bits - 2)) and $3f;
          // bits -= 2;
                    dec(bits,2);
    if (bits=0) then
                    begin
             {
    *step++ = b + 0x3c;
    b = 0;
    bits = 6;
         }
                         step^:=Chr(b+$3c);
                         inc(step);
                         b:=0;
                         bits:=6;
                    end;
    end;
    if (bits <> 6) then
    begin
     step^:=Chr(b+$3c);
                     inc(step);
    end; result:=step - text;
    end;
      

  2.   

    function mir_decode(binary,text:byte;size:integer):integer; //
    var
    step:byte;
    loop,bits:integer;
    idx:byte;
    begin
    step:=binary;
    bits:=2;

    for loop :=1 to size do
    begin
    idx:=INC(text)-$3c;

    if (idx<0) or idx>=64 then
    begin
    result:=-1;
    end; if bits<>2 then
    begin
    step[-1]:=(step[-1]) or (idx shr (8-bits));
    end; if bits<>8 then
    begin
    step:=idx shl bits;
    inc(step);
    inc(bits,2);
    end
    else
    bits:=2;
    end;
    end;

    if (size and 3)=1 then
    begin
    DEC(step);
    end; result:=step-binary;

    end;function mir_encode(text,binary:byte,size:integer):integer;
    var
    step,b,idx:byte;
    loop,bits:integer;

    begin
    step:=text;
    b:=0;
    bits:=6;

    for loop :=1 to size do
    begin
    idx:=INC(binary);
    step:=((idx shr (8-bits)) or b) + $3c;
    inc(step);
    b=(idx shl (bits-2)) and $3f;
    bits:=bits-2;

    if bits=0 then
    begin
    INC(step):=b+$3c;
    end;

    result:=step-text;
    end;
    end;随手翻的,肯定有错,有什么不清楚的,跟贴