求懂delphi的大佬帮忙翻译
看了半天 很多关键字都不认识procedure Encode6BitBuf(pSrc, PDest: PChar; nSrcLen, nDestLen: Integer);
var
i: Integer;
nRestCount: Integer;
nDestPos: Integer;
btMade: byte;
btCh: byte;
btRest: byte;
btXor: Byte;
begin
nRestCount := 0;
btRest := 0;
nDestPos := 0;
for i := 0 to nSrcLen - 1 do
begin
if nDestPos >= nDestLen then
break;
btCh := byte(pSrc[i]);
{$IF ENDECODEMODE = NEWMODE}
btXor := BITMASKS;
Inc(btXor, i);
btCh := btCh xor btXor;
{$IFEND}
btMade := byte((btRest or (btCh shr (2 + nRestCount))) and $3F);
btRest := byte(((btCh shl (8 - (2 + nRestCount))) shr 2) and $3F);
Inc(nRestCount, 2); if nRestCount < 6 then
begin
PDest[nDestPos] := Char(btMade + $3C);
Inc(nDestPos);
end
else
begin
if nDestPos < nDestLen - 1 then
begin
PDest[nDestPos] := Char(btMade + $3C);
PDest[nDestPos + 1] := Char(btRest + $3C);
Inc(nDestPos, 2);
end
else
begin
PDest[nDestPos] := Char(btMade + $3C);
Inc(nDestPos);
end;
nRestCount := 0;
btRest := 0;
end;
end;
if nRestCount > 0 then
begin
PDest[nDestPos] := Char(btRest + $3C);
Inc(nDestPos);
end;
PDest[nDestPos] := #0;
end;procedure Decode6BitBuf(sSource: PChar; pbuf: PChar; nSrcLen, nBufLen: Integer);
const
Masks: array[2..6] of byte = ($FC, $F8, $F0, $E0, $C0);
var
i, nBitPos, nMadeBit, nBufPos: Integer;
btCh, btTmp, btByte, btXor: byte;
begin
nBitPos := 2;
nMadeBit := 0;
nBufPos := 0;
btTmp := 0;
btCh := 0;
for i := 0 to nSrcLen - 1 do
begin
if Integer(sSource[i]) - $3C >= 0 then
btCh := byte(sSource[i]) - $3C
else
begin
nBufPos := 0;
break;
end;
if nBufPos >= nBufLen then
break;
if (nMadeBit + 6) >= 8 then
begin
btByte := byte(btTmp or ((btCh and $3F) shr (6 - nBitPos)));
{$IF ENDECODEMODE = NEWMODE}
btXor := BITMASKS;
Inc(btXor, nBufPos);
btByte := btByte xor btXor;
{$IFEND}
pbuf[nBufPos] := Char(btByte);
Inc(nBufPos);
nMadeBit := 0;
if nBitPos < 6 then
Inc(nBitPos, 2)
else
begin
nBitPos := 2;
continue;
end;
end;
btTmp := byte(byte(btCh shl nBitPos) and Masks[nBitPos]);
Inc(nMadeBit, 8 - nBitPos);
end;
pbuf[nBufPos] := #0;
end;
看了半天 很多关键字都不认识procedure Encode6BitBuf(pSrc, PDest: PChar; nSrcLen, nDestLen: Integer);
var
i: Integer;
nRestCount: Integer;
nDestPos: Integer;
btMade: byte;
btCh: byte;
btRest: byte;
btXor: Byte;
begin
nRestCount := 0;
btRest := 0;
nDestPos := 0;
for i := 0 to nSrcLen - 1 do
begin
if nDestPos >= nDestLen then
break;
btCh := byte(pSrc[i]);
{$IF ENDECODEMODE = NEWMODE}
btXor := BITMASKS;
Inc(btXor, i);
btCh := btCh xor btXor;
{$IFEND}
btMade := byte((btRest or (btCh shr (2 + nRestCount))) and $3F);
btRest := byte(((btCh shl (8 - (2 + nRestCount))) shr 2) and $3F);
Inc(nRestCount, 2); if nRestCount < 6 then
begin
PDest[nDestPos] := Char(btMade + $3C);
Inc(nDestPos);
end
else
begin
if nDestPos < nDestLen - 1 then
begin
PDest[nDestPos] := Char(btMade + $3C);
PDest[nDestPos + 1] := Char(btRest + $3C);
Inc(nDestPos, 2);
end
else
begin
PDest[nDestPos] := Char(btMade + $3C);
Inc(nDestPos);
end;
nRestCount := 0;
btRest := 0;
end;
end;
if nRestCount > 0 then
begin
PDest[nDestPos] := Char(btRest + $3C);
Inc(nDestPos);
end;
PDest[nDestPos] := #0;
end;procedure Decode6BitBuf(sSource: PChar; pbuf: PChar; nSrcLen, nBufLen: Integer);
const
Masks: array[2..6] of byte = ($FC, $F8, $F0, $E0, $C0);
var
i, nBitPos, nMadeBit, nBufPos: Integer;
btCh, btTmp, btByte, btXor: byte;
begin
nBitPos := 2;
nMadeBit := 0;
nBufPos := 0;
btTmp := 0;
btCh := 0;
for i := 0 to nSrcLen - 1 do
begin
if Integer(sSource[i]) - $3C >= 0 then
btCh := byte(sSource[i]) - $3C
else
begin
nBufPos := 0;
break;
end;
if nBufPos >= nBufLen then
break;
if (nMadeBit + 6) >= 8 then
begin
btByte := byte(btTmp or ((btCh and $3F) shr (6 - nBitPos)));
{$IF ENDECODEMODE = NEWMODE}
btXor := BITMASKS;
Inc(btXor, nBufPos);
btByte := btByte xor btXor;
{$IFEND}
pbuf[nBufPos] := Char(btByte);
Inc(nBufPos);
nMadeBit := 0;
if nBitPos < 6 then
Inc(nBitPos, 2)
else
begin
nBitPos := 2;
continue;
end;
end;
btTmp := byte(byte(btCh shl nBitPos) and Masks[nBitPos]);
Inc(nMadeBit, 8 - nBitPos);
end;
pbuf[nBufPos] := #0;
end;
shr 这个在C#里是<<
inc按照字面理解是自增1,相当于++
bxor是^
别的不懂的你再问
char btCh, btXor;
byte btRest, btMade;
// nRestCount := 0;
nRestCount = 0;
// btRest := 0;
btRest = 0;
// nDestPos := 0;
nDestPos = 0;
//for i := 0 to nSrcLen - 1 do
for (i = 0; i < nSrcLen; i++)
{
//if nDestPos >= nDestLen
if(nDestPos>= nDestLen)
{
break;
}
btCh = pSrc[i];
// btMade := byte((btRest or (btCh shr (2 + nRestCount))) and $3F);
btMade = 0;
// btRest := byte(((btCh shl (8 - (2 + nRestCount))) shr 2) and $3F);
btRest = 0;
//Inc(nRestCount, 2);
nRestCount += 2;
// if nRestCount < 6 then
if(nRestCount<6)
{
//PDest[nDestPos] := Char(btMade + $3C);
PDest[nDestPos] = (char)(btMade + 0x3C);
//Inc(nDestPos);
nDestPos++;
}
else
{
//if nDestPos < nDestLen - 1
if (nDestPos < nDestLen - 1)
{
PDest[nDestPos] = (char)(btMade + 0x3C);
PDest[nDestPos + 1] = (char)(btRest + 0x3C);
nDestPos += 2;
}else
{
PDest[nDestPos] = (char)(btMade + 0x3C);
nDestPos++;
}
nRestCount= 0;
btRest= 0;
} if (nRestCount > 0) { PDest[nDestPos] = (char)(btRest + 0x3C);
nDestPos++;
}
PDest[nDestPos] = (char)0;
}就差2句了.实在看不懂
麻烦帮忙看一下是否正确
C# btMade = (byte)((btRest | (btCh >> (2 + nRestCount))) & 0x3F);// btRest := byte(((btCh shl (8 - (2 + nRestCount))) shr 2) and $3F);
C# btRest = (byte)(((btCh << (8 - (2 + nRestCount))) >> 2) & 0x3F);就是这个意思:
01234567 01234567 01234567
012345 670123 456701 234567
应该就是 base64 的编码和解码
你验证一下