先把数据做一个base64编码吧。然后再存储到CString中 LONG CEncoder::encodeBase64A(const BYTE *in_buf, int in_len, LPCSTR sep, char* out_buf, DWORD *out_len) { int div, i; const BYTE *d = in_buf; int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0; DWORD needed; LPSTR ptr; TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes); needed = bytes + pad_bytes + 1; needed += (needed / 64 + 1) * strlen(sep); if (needed > *out_len) { *out_len = needed; return ERROR_INSUFFICIENT_BUFFER; } else *out_len = needed; /* Three bytes of input give 4 chars of output */ div = in_len / 3; ptr = out_buf; i = 0; while (div > 0) { if (i && i % 64 == 0) { strcpy(ptr, sep); ptr += strlen(sep); } /* first char is the first 6 bits of the first byte*/ *ptr++ = b64[ ( d[0] >> 2) & 0x3f ]; /* second char is the last 2 bits of the first byte and the first 4 * bits of the second byte */ *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)]; /* third char is the last 4 bits of the second byte and the first 2 * bits of the third byte */ *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)]; /* fourth char is the remaining 6 bits of the third byte */ *ptr++ = b64[ d[2] & 0x3f]; i += 4; d += 3; div--; } switch(pad_bytes) { case 1: /* first char is the first 6 bits of the first byte*/ *ptr++ = b64[ ( d[0] >> 2) & 0x3f ]; /* second char is the last 2 bits of the first byte and the first 4 * bits of the second byte */ *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)]; /* third char is the last 4 bits of the second byte padded with * two zeroes */ *ptr++ = b64[ ((d[1] << 2) & 0x3c) ]; /* fourth char is a = to indicate one byte of padding */ *ptr++ = '='; break; case 2: /* first char is the first 6 bits of the first byte*/ *ptr++ = b64[ ( d[0] >> 2) & 0x3f ]; /* second char is the last 2 bits of the first byte padded with * four zeroes*/ *ptr++ = b64[ ((d[0] << 4) & 0x30)]; /* third char is = to indicate padding */ *ptr++ = '='; /* fourth char is = to indicate padding */ *ptr++ = '='; break; } strcpy(ptr, sep); return ERROR_SUCCESS; }LONG CEncoder::decodeBase64Block(const char *in_buf, int in_len, const char **nextBlock, PBYTE out_buf, DWORD *out_len) { int len = in_len, i; const char *d = in_buf; int ip0, ip1, ip2, ip3;
if (len < 4) return ERROR_INVALID_DATA;
i = 0; if (d[2] == '=') { if ((ip0 = decodeBase64Byte(d[0])) > 63) return ERROR_INVALID_DATA; if ((ip1 = decodeBase64Byte(d[1])) > 63) return ERROR_INVALID_DATA;
if (out_buf) out_buf[i] = (ip0 << 2) | (ip1 >> 4); i++; } else if (d[3] == '=') { if ((ip0 = decodeBase64Byte(d[0])) > 63) return ERROR_INVALID_DATA; if ((ip1 = decodeBase64Byte(d[1])) > 63) return ERROR_INVALID_DATA; if ((ip2 = decodeBase64Byte(d[2])) > 63) return ERROR_INVALID_DATA;
if (c >= 'A' && c <= 'Z') ret = c - 'A'; else if (c >= 'a' && c <= 'z') ret = c - 'a' + 26; else if (c >= '0' && c <= '9') ret = c - '0' + 52; else if (c == '+') ret = 62; else if (c == '/') ret = 63; else ret = 64; return ret; }
CSTRING上,不知道这个问题怎么解决。
用得方便外,其他的还不怎么好用。
根据 lz 的描述,这个是不错的方法不知 lz 到底看上了 CString 哪点说说看,给个示例
,或者数组的话就比较麻烦。
,或者数组的话就比较麻烦//for循环而已
LONG CEncoder::encodeBase64A(const BYTE *in_buf, int in_len, LPCSTR sep, char* out_buf, DWORD *out_len)
{
int div, i;
const BYTE *d = in_buf;
int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
DWORD needed;
LPSTR ptr; TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
needed = bytes + pad_bytes + 1;
needed += (needed / 64 + 1) * strlen(sep); if (needed > *out_len)
{
*out_len = needed;
return ERROR_INSUFFICIENT_BUFFER;
}
else
*out_len = needed; /* Three bytes of input give 4 chars of output */
div = in_len / 3; ptr = out_buf;
i = 0;
while (div > 0)
{
if (i && i % 64 == 0)
{
strcpy(ptr, sep);
ptr += strlen(sep);
}
/* first char is the first 6 bits of the first byte*/
*ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
/* second char is the last 2 bits of the first byte and the first 4
* bits of the second byte */
*ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
/* third char is the last 4 bits of the second byte and the first 2
* bits of the third byte */
*ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
/* fourth char is the remaining 6 bits of the third byte */
*ptr++ = b64[ d[2] & 0x3f];
i += 4;
d += 3;
div--;
} switch(pad_bytes)
{
case 1:
/* first char is the first 6 bits of the first byte*/
*ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
/* second char is the last 2 bits of the first byte and the first 4
* bits of the second byte */
*ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
/* third char is the last 4 bits of the second byte padded with
* two zeroes */
*ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
/* fourth char is a = to indicate one byte of padding */
*ptr++ = '=';
break;
case 2:
/* first char is the first 6 bits of the first byte*/
*ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
/* second char is the last 2 bits of the first byte padded with
* four zeroes*/
*ptr++ = b64[ ((d[0] << 4) & 0x30)];
/* third char is = to indicate padding */
*ptr++ = '=';
/* fourth char is = to indicate padding */
*ptr++ = '=';
break;
}
strcpy(ptr, sep); return ERROR_SUCCESS;
}LONG CEncoder::decodeBase64Block(const char *in_buf, int in_len, const char **nextBlock, PBYTE out_buf, DWORD *out_len)
{
int len = in_len, i;
const char *d = in_buf;
int ip0, ip1, ip2, ip3;
if (len < 4)
return ERROR_INVALID_DATA;
i = 0;
if (d[2] == '=')
{
if ((ip0 = decodeBase64Byte(d[0])) > 63)
return ERROR_INVALID_DATA;
if ((ip1 = decodeBase64Byte(d[1])) > 63)
return ERROR_INVALID_DATA;
if (out_buf)
out_buf[i] = (ip0 << 2) | (ip1 >> 4);
i++;
}
else if (d[3] == '=')
{
if ((ip0 = decodeBase64Byte(d[0])) > 63)
return ERROR_INVALID_DATA;
if ((ip1 = decodeBase64Byte(d[1])) > 63)
return ERROR_INVALID_DATA;
if ((ip2 = decodeBase64Byte(d[2])) > 63)
return ERROR_INVALID_DATA;
if (out_buf)
{
out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
}
i += 2;
}
else
{
if ((ip0 = decodeBase64Byte(d[0])) > 63)
return ERROR_INVALID_DATA;
if ((ip1 = decodeBase64Byte(d[1])) > 63)
return ERROR_INVALID_DATA;
if ((ip2 = decodeBase64Byte(d[2])) > 63)
return ERROR_INVALID_DATA;
if ((ip3 = decodeBase64Byte(d[3])) > 63)
return ERROR_INVALID_DATA;
if (out_buf)
{
out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
out_buf[i + 2] = (ip2 << 6) | ip3;
}
i += 3;
}
if (len >= 6 && d[4] == '\r' && d[5] == '\n')
*nextBlock = d + 6;
else if (len >= 5 && d[4] == '\n')
*nextBlock = d + 5;
else if (len >= 4 && d[4])
*nextBlock = d + 4;
else
*nextBlock = NULL;
*out_len = i;
return ERROR_SUCCESS;
}BYTE inline CEncoder::decodeBase64Byte(char c)
{
BYTE ret;
if (c >= 'A' && c <= 'Z')
ret = c - 'A';
else if (c >= 'a' && c <= 'z')
ret = c - 'a' + 26;
else if (c >= '0' && c <= '9')
ret = c - '0' + 52;
else if (c == '+')
ret = 62;
else if (c == '/')
ret = 63;
else
ret = 64;
return ret;
}