在做邮件接收系统
从POP3服务器下载下来的邮件,有些信息是Base64编程的需要解码。
如:
From: "=?GBK?B?UVHTys/kudzA7dSx?=" <10000@qq.com>
To: 236993171@qq.com
Subject: =?GBK?B?U01UULf+zvHJ6NbDzOHQ0Q==?=
解码成:
From: QQ邮箱管理员<10000@qq.com>
To: 236993171@qq.com
Subject: SMTP服务设置提醒
实际操作解码后字符串有问题:
From: 丵Q邮箱管理员<10000@qq.com>
To: 236993171@qq.com
Subject: 丼MTP服务设置提醒
求大神解析,解码的时候需要把=加进去吗? 2者都试过了,不行
------解决方案--------------------------------------------------------
仅供参考
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BASE64_VALUE_SZ 256
int base64_value[BASE64_VALUE_SZ];
const unsigned char alphabet[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
class Base64Utility {
public:
Base64Utility();
int base64_encode(char *src, int srclen, char *dst, int tail);
int base64_decode(char *src, int srclen, char *dst);
private:
void base_64_init(void);
};
Base64Utility::Base64Utility() {
base_64_init();
}
void Base64Utility::base_64_init(void) {
int i;
for (i = 0; i < BASE64_VALUE_SZ; i++) base64_value[i] = -1;
for (i = 0; i < 64; i++) base64_value[(int) alphabet[i]] = i;
base64_value['='] = 0;
}
int Base64Utility::base64_encode(char *src, int srclen, char *dst, int tail) {
int bits, char_count, len;
char *o_char, *lim, *o_lim;
unsigned char c;
if ( !src
------解决方案--------------------------------------------------------
!dst) return 0;
len = srclen;
lim = src + len;
o_char = dst;
o_lim = dst + (len*4)/3 + 1;
char_count = 0;
bits = 0;
while ( (src < lim) && (o_char < o_lim)) {
c = *(src++);
bits += c;
char_count++;
if (char_count == 3) {
*(o_char++) = alphabet[bits >> 18];
*(o_char++) = alphabet[(bits >> 12) & 0x3f];
*(o_char++) = alphabet[(bits >> 6) & 0x3f];