请各位熟悉VC的朋友进来看一下.把其转成DELPHI ( 积分: 200 )

  • 主题发起人 主题发起人 ic
  • 开始时间 开始时间
I

ic

Unregistered / Unconfirmed
GUEST, unregistred user!
/=======================================================================
// Base64Encode/Base64Decode
// compliant with RFC 2045
//=======================================================================
//
#define ATL_BASE64_FLAG_NONE 0
#define ATL_BASE64_FLAG_NOPAD 1
#define ATL_BASE64_FLAG_NOCRLF 2

inline int Base64EncodeGetRequiredLength(int nSrcLen, DWORD dwFlags=ATL_BASE64_FLAG_NONE) throw()
{
int nRet = nSrcLen*4/3;

if ((dwFlags & ATL_BASE64_FLAG_NOPAD) == 0)
nRet += nSrcLen % 3;

int nCRLFs = nRet / 76 + 1;
int nOnLastLine = nRet % 76;

if (nOnLastLine)
{
if (nOnLastLine % 4)
nRet += 4-(nOnLastLine % 4);
}

nCRLFs *= 2;

if ((dwFlags & ATL_BASE64_FLAG_NOCRLF) == 0)
nRet += nCRLFs;

return nRet;
}

inline int Base64DecodeGetRequiredLength(int nSrcLen) throw()
{
return nSrcLen;
}

inline BOOL Base64Encode(
const BYTE *pbSrcData,
int nSrcLen,
LPSTR szDest,
int *pnDestLen,
DWORD dwFlags=ATL_BASE64_FLAG_NONE) throw()
{
static const char s_chBase64EncodingTable[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

if (!pbSrcData || !szDest || !pnDestLen)
{
return FALSE;
}

if(*pnDestLen < Base64EncodeGetRequiredLength(nSrcLen, dwFlags))
{
ASSERT(FALSE);
return FALSE;
}

int nWritten( 0 );
int nLen1( (nSrcLen/3)*4 );
int nLen2( nLen1/76 );
int nLen3( 19 );

for (int i=0; i<=nLen2; i++)
{
if (i==nLen2)
nLen3 = (nLen1%76)/4;

for (int j=0; j<nLen3; j++)
{
DWORD dwCurr(0);
for (int n=0; n<3; n++)
{
dwCurr |= *pbSrcData++;
dwCurr <<= 8;
}
for (int k=0; k<4; k++)
{
BYTE b = (BYTE)(dwCurr>>26);
*szDest++ = s_chBase64EncodingTable;
dwCurr <<= 6;
}
}
nWritten+= nLen3*4;

if ((dwFlags & ATL_BASE64_FLAG_NOCRLF)==0)
{
*szDest++ = '/r';
*szDest++ = '/n';
nWritten+= 2;
}
}

if (nWritten && (dwFlags & ATL_BASE64_FLAG_NOCRLF)==0)
{
szDest-= 2;
nWritten -= 2;
}

nLen2 = nSrcLen%3 ? nSrcLen%3 + 1 : 0;
if (nLen2)
{
DWORD dwCurr(0);
for (int n=0; n<3; n++)
{
if (n<(nSrcLen%3))
dwCurr |= *pbSrcData++;
dwCurr <<= 8;
}
for (int k=0; k<nLen2; k++)
{
BYTE b = (BYTE)(dwCurr>>26);
*szDest++ = s_chBase64EncodingTable;
dwCurr <<= 6;
}
nWritten+= nLen2;
if ((dwFlags & ATL_BASE64_FLAG_NOPAD)==0)
{
nLen3 = nLen2 ? 4-nLen2 : 0;
for (int j=0; j<nLen3; j++)
{
*szDest++ = '=';
}
nWritten+= nLen3;
}
}

*pnDestLen = nWritten;
return TRUE;
}

inline int DecodeBase64Char(unsigned int ch) throw()
{
// returns -1 if the character is invalid
// or should be skipped
// otherwise, returns the 6-bit code for the character
// from the encoding table
if (ch >= 'A' && ch <= 'Z')
return ch - 'A' + 0; // 0 range starts at 'A'
if (ch >= 'a' && ch <= 'z')
return ch - 'a' + 26; // 26 range starts at 'a'
if (ch >= '0' && ch <= '9')
return ch - '0' + 52; // 52 range starts at '0'
if (ch == '+')
return 62;
if (ch == '/')
return 63;
return -1;
}

inline BOOL Base64Decode(LPCSTR szSrc, int nSrcLen, BYTE *pbDest, int *pnDestLen) throw()
{
// walk the source buffer
// each four character sequence is converted to 3 bytes
// CRLFs and =, and any characters not in the encoding table
// are skiped

if (szSrc == NULL || pnDestLen == NULL)
{
ASSERT(FALSE);
return FALSE;
}

LPCSTR szSrcEnd = szSrc + nSrcLen;
int nWritten = 0;

BOOL bOverflow = (pbDest == NULL) ? TRUE : FALSE;

while (szSrc < szSrcEnd)
{
DWORD dwCurr = 0;
int i;
int nBits = 0;
for (i=0; i<4; i++)
{
if (szSrc >= szSrcEnd)
break;
int nCh = DecodeBase64Char(*szSrc);
szSrc++;
if (nCh == -1)
{
// skip this char
i--;
continue;
}
dwCurr <<= 6;
dwCurr |= nCh;
nBits += 6;
}

if(!bOverflow && nWritten + (nBits/8) > (*pnDestLen))
bOverflow = TRUE;

// dwCurr has the 3 bytes to write to the output buffer
// left to right
dwCurr <<= 24-nBits;
for (i=0; i<nBits/8; i++)
{
if(!bOverflow)
{
*pbDest = (BYTE) ((dwCurr & 0x00ff0000) >> 16);
pbDest++;
}
dwCurr <<= 8;
nWritten++;
}
}

*pnDestLen = nWritten;

if(bOverflow)
{
if(pbDest != NULL)
ASSERT(FALSE);

return FALSE;
}

return TRUE;
}
 
/=======================================================================
// Base64Encode/Base64Decode
// compliant with RFC 2045
//=======================================================================
//
#define ATL_BASE64_FLAG_NONE 0
#define ATL_BASE64_FLAG_NOPAD 1
#define ATL_BASE64_FLAG_NOCRLF 2

inline int Base64EncodeGetRequiredLength(int nSrcLen, DWORD dwFlags=ATL_BASE64_FLAG_NONE) throw()
{
int nRet = nSrcLen*4/3;

if ((dwFlags & ATL_BASE64_FLAG_NOPAD) == 0)
nRet += nSrcLen % 3;

int nCRLFs = nRet / 76 + 1;
int nOnLastLine = nRet % 76;

if (nOnLastLine)
{
if (nOnLastLine % 4)
nRet += 4-(nOnLastLine % 4);
}

nCRLFs *= 2;

if ((dwFlags & ATL_BASE64_FLAG_NOCRLF) == 0)
nRet += nCRLFs;

return nRet;
}

inline int Base64DecodeGetRequiredLength(int nSrcLen) throw()
{
return nSrcLen;
}

inline BOOL Base64Encode(
const BYTE *pbSrcData,
int nSrcLen,
LPSTR szDest,
int *pnDestLen,
DWORD dwFlags=ATL_BASE64_FLAG_NONE) throw()
{
static const char s_chBase64EncodingTable[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

if (!pbSrcData || !szDest || !pnDestLen)
{
return FALSE;
}

if(*pnDestLen < Base64EncodeGetRequiredLength(nSrcLen, dwFlags))
{
ASSERT(FALSE);
return FALSE;
}

int nWritten( 0 );
int nLen1( (nSrcLen/3)*4 );
int nLen2( nLen1/76 );
int nLen3( 19 );

for (int i=0; i<=nLen2; i++)
{
if (i==nLen2)
nLen3 = (nLen1%76)/4;

for (int j=0; j<nLen3; j++)
{
DWORD dwCurr(0);
for (int n=0; n<3; n++)
{
dwCurr |= *pbSrcData++;
dwCurr <<= 8;
}
for (int k=0; k<4; k++)
{
BYTE b = (BYTE)(dwCurr>>26);
*szDest++ = s_chBase64EncodingTable;
dwCurr <<= 6;
}
}
nWritten+= nLen3*4;

if ((dwFlags & ATL_BASE64_FLAG_NOCRLF)==0)
{
*szDest++ = '/r';
*szDest++ = '/n';
nWritten+= 2;
}
}

if (nWritten && (dwFlags & ATL_BASE64_FLAG_NOCRLF)==0)
{
szDest-= 2;
nWritten -= 2;
}

nLen2 = nSrcLen%3 ? nSrcLen%3 + 1 : 0;
if (nLen2)
{
DWORD dwCurr(0);
for (int n=0; n<3; n++)
{
if (n<(nSrcLen%3))
dwCurr |= *pbSrcData++;
dwCurr <<= 8;
}
for (int k=0; k<nLen2; k++)
{
BYTE b = (BYTE)(dwCurr>>26);
*szDest++ = s_chBase64EncodingTable;
dwCurr <<= 6;
}
nWritten+= nLen2;
if ((dwFlags & ATL_BASE64_FLAG_NOPAD)==0)
{
nLen3 = nLen2 ? 4-nLen2 : 0;
for (int j=0; j<nLen3; j++)
{
*szDest++ = '=';
}
nWritten+= nLen3;
}
}

*pnDestLen = nWritten;
return TRUE;
}

inline int DecodeBase64Char(unsigned int ch) throw()
{
// returns -1 if the character is invalid
// or should be skipped
// otherwise, returns the 6-bit code for the character
// from the encoding table
if (ch >= 'A' && ch <= 'Z')
return ch - 'A' + 0; // 0 range starts at 'A'
if (ch >= 'a' && ch <= 'z')
return ch - 'a' + 26; // 26 range starts at 'a'
if (ch >= '0' && ch <= '9')
return ch - '0' + 52; // 52 range starts at '0'
if (ch == '+')
return 62;
if (ch == '/')
return 63;
return -1;
}

inline BOOL Base64Decode(LPCSTR szSrc, int nSrcLen, BYTE *pbDest, int *pnDestLen) throw()
{
// walk the source buffer
// each four character sequence is converted to 3 bytes
// CRLFs and =, and any characters not in the encoding table
// are skiped

if (szSrc == NULL || pnDestLen == NULL)
{
ASSERT(FALSE);
return FALSE;
}

LPCSTR szSrcEnd = szSrc + nSrcLen;
int nWritten = 0;

BOOL bOverflow = (pbDest == NULL) ? TRUE : FALSE;

while (szSrc < szSrcEnd)
{
DWORD dwCurr = 0;
int i;
int nBits = 0;
for (i=0; i<4; i++)
{
if (szSrc >= szSrcEnd)
break;
int nCh = DecodeBase64Char(*szSrc);
szSrc++;
if (nCh == -1)
{
// skip this char
i--;
continue;
}
dwCurr <<= 6;
dwCurr |= nCh;
nBits += 6;
}

if(!bOverflow && nWritten + (nBits/8) > (*pnDestLen))
bOverflow = TRUE;

// dwCurr has the 3 bytes to write to the output buffer
// left to right
dwCurr <<= 24-nBits;
for (i=0; i<nBits/8; i++)
{
if(!bOverflow)
{
*pbDest = (BYTE) ((dwCurr & 0x00ff0000) >> 16);
pbDest++;
}
dwCurr <<= 8;
nWritten++;
}
}

*pnDestLen = nWritten;

if(bOverflow)
{
if(pbDest != NULL)
ASSERT(FALSE);

return FALSE;
}

return TRUE;
}
 
正在翻译中……

先给出转的一个代码,如果对你有用,我就不翻译了
http://www.delphibbs.com/delphibbs/dispq.asp?lid=525820

unit Pub;

interface

uses
Math,SysUtils;

function Base64CodeTab(AIndex: Byte): string;
function Base64Decode(AInputStr: string): string;
function Base64DecodeTab(AIndex: Char): Byte;
function Base64Encode(AInputStr: string): string;
function BinToInt(ABinStr: string): Integer;
function HexToBinary(AHexStr: string): string;

implementation

function Base64CodeTab(AIndex: Byte): string;
begin
Result := '';
case AIndex of
0: Result := 'A';
1: Result := 'B';
2: Result := 'C';
3: Result := 'D';
4: Result := 'E';
5: Result := 'F';
6: Result := 'G';
7: Result := 'H';
8: Result := 'I';
9: Result := 'J';
10: Result := 'K';
11: Result := 'L';
12: Result := 'M';
13: Result := 'N';
14: Result := 'O';
15: Result := 'P';
16: Result := 'Q';
17: Result := 'R';
18: Result := 'S';
19: Result := 'T';
20: Result := 'U';
21: Result := 'V';
22: Result := 'W';
23: Result := 'X';
24: Result := 'Y';
25: Result := 'Z';
26: Result := 'a';
27: Result := 'b';
28: Result := 'c';
29: Result := 'd';
30: Result := 'e';
31: Result := 'f';
32: Result := 'g';
33: Result := 'h';
34: Result := 'i';
35: Result := 'j';
36: Result := 'k';
37: Result := 'l';
38: Result := 'm';
39: Result := 'n';
40: Result := 'o';
41: Result := 'p';
42: Result := 'q';
43: Result := 'r';
44: Result := 's';
45: Result := 't';
46: Result := 'u';
47: Result := 'v';
48: Result := 'w';
49: Result := 'x';
50: Result := 'y';
51: Result := 'z';
52: Result := '0';
53: Result := '1';
54: Result := '2';
55: Result := '3';
56: Result := '4';
57: Result := '5';
58: Result := '6';
59: Result := '7';
60: Result := '8';
61: Result := '9';
62: Result := '+';
63: Result := '/';
end;
end;

function Base64Decode(AInputStr: string): string;
var
i: Integer;
ABuffer: string[24];
begin
Result := '';
while Length(AInputStr) > 4 do
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[4]),2)),3,6);
for i := 0 to 2 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
AInputStr := Copy(AInputStr,5,Length(AInputStr));
end;
case Pos('=',AInputStr) of
3:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6);
Result := Result + Chr(BinToInt(Copy(ABuffer,1,8)));
end;
4:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6);
for i := 0 to 1 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
end;
0:
begin
ABuffer := Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[1]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[2]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[3]),2)),3,6)
+ Copy(HexToBinary(IntToHex(Base64DecodeTab(AInputStr[4]),2)),3,6);
for i := 0 to 2 do
Result := Result + Chr(BinToInt(Copy(ABuffer,i*8+1,8)));
end;
end;
end;

function Base64DecodeTab(AIndex: Char): Byte;
begin
Result := 0;
case AIndex of
'A': Result := 0;
'B': Result := 1;
'C': Result := 2;
'D': Result := 3;
'E': Result := 4;
'F': Result := 5;
'G': Result := 6;
'H': Result := 7;
'I': Result := 8;
'J': Result := 9;
'K': Result := 10;
'L': Result := 11;
'M': Result := 12;
'N': Result := 13;
'O': Result := 14;
'P': Result := 15;
'Q': Result := 16;
'R': Result := 17;
'S': Result := 18;
'T': Result := 19;
'U': Result := 20;
'V': Result := 21;
'W': Result := 22;
'X': Result := 23;
'Y': Result := 24;
'Z': Result := 25;
'a': Result := 26;
'b': Result := 27;
'c': Result := 28;
'd': Result := 29;
'e': Result := 30;
'f': Result := 31;
'g': Result := 32;
'h': Result := 33;
'i': Result := 34;
'j': Result := 35;
'k': Result := 36;
'l': Result := 37;
'm': Result := 38;
'n': Result := 39;
'o': Result := 40;
'p': Result := 41;
'q': Result := 42;
'r': Result := 43;
's': Result := 44;
't': Result := 45;
'u': Result := 46;
'v': Result := 47;
'w': Result := 48;
'x': Result := 49;
'y': Result := 50;
'z': Result := 51;
'0': Result := 52;
'1': Result := 53;
'2': Result := 54;
'3': Result := 55;
'4': Result := 56;
'5': Result := 57;
'6': Result := 58;
'7': Result := 59;
'8': Result := 60;
'9': Result := 61;
'+': Result := 62;
'/': Result := 63;
end;
end;

function Base64Encode(AInputStr: string): string;
var
i: Integer;
ABuffer: string[24];
begin
Result := '';
while Length(AInputStr) > 3 do
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[3]),2));
for i := 0 to 3 do
begin
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)))
end;
AInputStr := Copy(AInputStr,4,Length(AInputStr));
end;
case Length(AInputStr) of
1:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2)) + '0000';
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,1,6))) +
Base64CodeTab(BinToInt(Copy(ABuffer,7,6))) + '==';
end;
2:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2)) + '00';
for i := 0 to 2 do
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)));
Result := Result + '=';
end;
3:
begin
ABuffer := HexToBinary(IntToHex(Ord(AInputStr[1]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[2]),2))
+ HexToBinary(IntToHex(Ord(AInputStr[3]),2));
for i := 0 to 3 do
Result := Result + Base64CodeTab(BinToInt(Copy(ABuffer,i*6+1,6)))
end;
end;
end;

function BinToInt(ABinStr: string): Integer;
var
i: Integer;
begin
Result := 0;
for i:=0 to Length(ABinStr)-1 do
begin
Result := Result + StrToInt(ABinStr[i+1]) * Round(IntPower(2,Length(ABinStr)-i-1));
end;
end;

function HexToBinary(AHexStr: string): string;
var
i: Integer;
begin
Result := '';
for i:=1 to Length(AHexStr) do
begin
case AHexStr of
'0':
begin
Result := Result + '0000';
end;
'1':
begin
Result := Result + '0001';
end;
'2':
begin
Result := Result + '0010';
end;
'3':
begin
Result := Result + '0011';
end;
'4':
begin
Result := Result + '0100';
end;
'5':
begin
Result := Result + '0101';
end;
'6':
begin
Result := Result + '0110';
end;
'7':
begin
Result := Result + '0111';
end;
'8':
begin
Result := Result + '1000';
end;
'9':
begin
Result := Result + '1001';
end;
'A':
begin
Result := Result + '1010';
end;
'B':
begin
Result := Result + '1011';
end;
'C':
begin
Result := Result + '1100';
end;
'D':
begin
Result := Result + '1101';
end;
'E':
begin
Result := Result + '1110';
end;
'F':
begin
Result := Result + '1111';
end;
end;
end;
end;

end.
 
.chenybin,老兄.感谢了.如果分不够,我另贴加.
你贴的代码我也有,只是与上面VC代码运行后的结果不一样,但由于是混合编程,必须要统一.
 
修改完成了,不知道对不对,不过能翻译成这样,你自己也已经很容易修改了,记得给我加分,搞了好久的,

pas文件
// 几个问题:记得C里面大于0就是表示真,然后是&记得是与吧,我直接用and了
// 其他的注释在代码里面

unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

const
ATL_BASE64_FLAG_NONE = 0;
ATL_BASE64_FLAG_NOPAD = 1;
ATL_BASE64_FLAG_NOCRLF = 2;

type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function Base64EncodeGetRequiredLength(nSrcLen: Integer; dwFlags: DWORD = ATL_BASE64_FLAG_NONE): Integer;
var
nRet: Integer;
nCRLFs, nOnLastLine: Integer;
begin
nRet := nSrcLen * 4 div 3;

if ((dwFlags and ATL_BASE64_FLAG_NOPAD) = 0) then
Inc(nRet, nSrcLen mod 3);

nCRLFs := nRet div 76 + 1;
nOnLastLine := nRet mod 76;

if (nOnLastLine > 0) then begin
if ((nOnLastLine mod 4) > 0) then
Inc(nRet, 4 - (nOnLastLine mod 4));
end;

nCRLFs := nCRLFs * 2;

if ((dwFlags and ATL_BASE64_FLAG_NOCRLF) = 0) then
Inc(nRet, nCRLFs);

Result := nRet;
end;

function Base64DecodeGetRequiredLength(nSrcLen: Integer): Integer;
begin
Result := nSrcLen;
end;

function Base64Encode(pbSrcData: PByte; nSrcLen: Integer; szDest: PChar;
pnDestLen: PInteger; dwFlags: DWORD =ATL_BASE64_FLAG_NONE): Bool;
const
s_chBase64EncodingTable: array[0..63] of char = (
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/');
var

I, J, n, K: Integer;

nWritten, nLen1, nLen2, nLen3: Integer;
dwCurr: DWORD;
b: BYTE;
begin
if (pbSrcData=nil) or (szDest = nil) or (pnDestLen= nil) then begin
Result := False;
Exit;
end;

if pnDestLen^ < Base64EncodeGetRequiredLength(nSrcLen, dwFlags) then begin
Result := False;
Exit;
end;

nWritten := 0;
nLen1 := (nSrcLen div 3) * 4;
nLen2 := nLen1 div 76;
nLen3 := 19;

for I := 0 to nLen2 - 1 do begin
if (I = nLen2) then
nLen3 := (nLen1 mod 76) div 4;

for J := 0 to nLen3 - 1 do begin
dwCurr := 0;
for n := 0 to 2 do begin
dwCurr := dwCurr or (pbSrcData^);
Inc(pbSrcData^);
dwCurr := dwCurr shl 8;
end;
for K := 0 to 3 do begin
b := BYTE(dwCurr shr 26);
Inc(szDest^, 1);
szDest^ := s_chBase64EncodingTable;
dwCurr := dwCurr shl 6;
end;
end;
Inc(nWritten, nLen3 * 4);

if ((dwFlags and ATL_BASE64_FLAG_NOCRLF) = 0) then begin
Inc(szDest^, 1);
szDest^ := #10;
Inc(szDest^, 1);
szDest^ := #13;
Inc(nWritten, 2);
end;
end;

if (nWritten > 0) and ((dwFlags or ATL_BASE64_FLAG_NOCRLF) = 0) then begin
Dec(szDest, 2);
Dec(nWritten, 2);
end;

if (nSrcLen mod 3) > 0 then
nLen2 := nSrcLen mod 3
else
nLen2 := 0;

if (nLen2 > 0) then begin
dwCurr := 0;
for n := 0 to 2 do begin
if (n < (nSrcLen mod 3)) then begin
dwCurr := dwCurr or pbSrcData^;
Inc(pbSrcData^);
end;

dwCurr := dwCurr shl 8;
end;
for K := 0 to nLen2 - 1 do begin
b := BYTE((dwCurr shr 26));
Inc(szDest^, 1);
szDest^ := s_chBase64EncodingTable;
dwCurr := dwCurr shl 6;
end;
Inc(nWritten, nLen2);
if ((dwFlags and ATL_BASE64_FLAG_NOPAD) = 0) then begin
if nLen2 > 0 then
nLen3 := 4 - nLen2
else
nLen3 := 0;
for J := 0 to nLen3 - 1 do begin
Inc(szDest^, 1);
szDest^ := '=';
end;

Inc(nWritten, nLen3);
end;
end;

pnDestLen^ := nWritten;
Result := TRUE;
end;

function DecodeBase64Char(ch: Word): Integer;
begin
// returns -1 if the character is invalid
// or should be skipped
// otherwise, returns the 6-bit code for the character
// from the encoding table
Result := -1;
if (ch >= Ord('A')) and (ch <= Ord('Z')) then
Result := ch - Ord('A') + 0; // 0 range starts at 'A'
if (ch >= Ord('a')) and (ch <= Ord('z')) then
Result := ch - Ord('a') + 26; // 26 range starts at 'a'
if (ch >= Ord('0')) and (ch <= Ord('9')) then
Result := ch - Ord('0') + 52; // 52 range starts at '0'
if (ch = Ord('+')) then
Result := 62;
if (ch = Ord('/')) then
Result := 63;
end;

function Base64Decode(szSrc: PChar; nSrcLen: Integer; pbDest: PByte; pnDestLen: PInteger): Boolean;
var
szSrcEnd: PChar;
nWritten: Integer;
bOverflow: Boolean;
dwCurr: DWORD;
I: Integer;
nBits: Integer;
nCh: Integer;
begin
Result := TRUE;

if (not Assigned(szSrc)) or (not Assigned(pnDestLen)) then
Exit;

szSrcEnd := szSrc + nSrcLen;
nWritten := 0;

if not Assigned(pbDest) then
bOverflow := TRUE
else
bOverflow := False;

while (szSrc < szSrcEnd) do begin
dwCurr := 0;
nBits := 0;
I := 0;
while I < 4 do begin // Delphi里面不支持对循环变量赋值。所以换成while循环
if (szSrc >= szSrcEnd) then
break;

// int nCh = DecodeBase64Char(*szSrc);这句可能改得不对
nCh := DecodeBase64Char(Ord(szSrc^));
Inc(szSrc);
if (nCh = -1) then begin
// skip this char
Dec(I);
continue;
end;
dwCurr := dwCurr shl 6;

// 下面这句有个编译警告
//Combining signed and unsigned types - widened both operands
// 估计是因为dwCurr为无符号,nCh为有符号吧

dwCurr := dwCurr or nCh;
Inc(nBits, 6);
Inc(I);
end;

if (not bOverflow) and (nWritten + (nBits div 8) > pnDestLen^) then
bOverflow := TRUE;

// dwCurr has the 3 bytes to write to the output buffer
// left to right
dwCurr := dwCurr shl (24 - nBits);
for I := 0 to (nBits div 8 - 1) do begin
if (not bOverflow) then begin
pbDest^ := BYTE(((dwCurr or $00FF0000) shr 16));
Inc(pbDest);
end;
dwCurr := dwCurr shl 8;
Inc(nWritten);
end;
end;

pnDestLen^ := nWritten;

if (bOverflow) then begin
Result := False;
end;
end;


end.

如果成功再加点分(脸红Ing……)[:D][:D][:D]
 
再次感谢chenybin老兄,但结果是不对的.本来1的BASE64是MQAAAAAAAAAAAAAAAAAAAA==
但结果是=.

调用:
BOOL bR = FALSE;
int i1 = 32, i2 = 16;
char cData[32] = {0, };
//=======================================
Base64Encode((BYTE *)cBuf, i2, cData, &i1, ATL_BASE64_FLAG_NONE);
CopyMemory(cBuf, cData, i1);
如果可以的话你在VC中试试结果.
 
把c做成dll,或者链接obj文件,比翻译这个方便的多
 
再次感谢chenybin老兄,但结果是不对的.本来1的BASE64是MQAAAAAAAAAAAAAAAAAAAA==
但结果是=.

调用:
BOOL bR = FALSE;
int i1 = 32, i2 = 16;
char cData[32] = {0, };
//=======================================
Base64Encode((BYTE *)cBuf, i2, cData, &i1, ATL_BASE64_FLAG_NONE);
CopyMemory(cBuf, cData, i1);
如果可以的话你在VC中试试结果.
 
我们这个项目以前是用VC作的.现要改为DELPHI,但密码读取要与老版本兼容.所以这样做.
 
BOOL bR = FALSE;
int i1 = 32, i2 = 16;
char cData[32];
char* cBuf = new char[32];

memset(cData, 0, 32);
UpdateData(true);
cBuf = (char*)m_strEncode.GetBuffer(32);
i2 = m_strEncode.GetLength();
m_strEncode.ReleaseBuffer(32);
m_strEncode = "";
Base64Encode((BYTE *)cBuf, i2, cData, &i1, ATL_BASE64_FLAG_NONE);
memcpy(cBuf, cData, i1);

m_strDecode = cBuf;
m_strEncode = "";
UpdateData(false);
cBuf = "";

m_strDecode,m_strEncode分别对应两个控件,为CString类型,但是结果不对,我想你自己里面还做了其他处理吧,不行我把VC的代码给你


BOOL bR = FALSE;
int i1 = 32, i2 = 16;
char cData[32];
char* cBuf = new char[32];

memset(cData, 0, 32);
UpdateData(true);
cBuf[0] = '0';
cBuf[1] = 'A';//{0, 'A'};//"0A";//(char*)m_strEncode.GetBuffer(32);
//i2 = m_strEncode.GetLength();
//m_strEncode.ReleaseBuffer(32);
//m_strEncode = "";
Base64Encode((BYTE *)cBuf, i2, cData, &i1, ATL_BASE64_FLAG_NONE);
memcpy(cBuf, cData, i1);

m_strDecode = cBuf;
//m_strEncode = "";
UpdateData(false);
cBuf = "";

对C不熟悉,结果是
MDHNzc3Nzc3Nzc3Nzc3NzQ==屯屯屯屯?
 
我按照你给的VC代码写的东西结果不对,我想应该还是调用的参数问题,我现在把工程发给你,Delphi的代码你应该比较熟悉,确实没有问题,而且你说的1我不知道应设置在哪里。尽力了,惭愧
 
后退
顶部