MIME之Quoted-Printable编解码 (转)

MIME之Quoted-Printable编解码 (转)[@more@]

Quoted-Printable也是MIME邮件中常用的编码方式之一。同Base64一样,它也将输入的字符串或数据编码成全是ASCII码的可打印字符串。

Quoted-Printable编码的基本方法是:输入数据在33-60、62-126范围内的,直接输出;其它的需编码为“=”加两个字节的HEX码(大写)。为保证输出行不超过规定长度,可在行尾加“= ”序列作为软回车。

int EncodeQuoted(const unsigned char* pSrc, char* pDst, int nSrcLen, int nMaxLineLen) { int nDstLen; // 输出的字符计数 int nLineLen; // 输出的行长度计数 nDstLen = 0; nLineLen = 0; for (int i = 0; i < nSrcLen; i++, pSrc++) { // ASCII 33-60, 62-126原样输出,其余的需编码 if ((*pSrc >= '!') && (*pSrc <= '~') && (*pSrc != '=')) { *pDst++ = (char)*pSrc; nDstLen++; nLineLen++; } else { sprintf(pDst, "=%02X", *pSrc); pDst += 3; nDstLen += 3; nLineLen += 3; } // 输出换行? if (nLineLen >= nMaxLineLen - 3) { sprintf(pDst, "= "); pDst += 3; nDstLen += 3; nLineLen = 0; } } // 输出加个结束符 *pDst = ''; return nDstLen; }

 
 

Quoted-Printable解码很简单,将编码过程反过来就行了。

int DecodeQuoted(const char* pSrc, unsigned char* pDst, int nSrcLen) { int nDstLen; // 输出的字符计数 int i; i = 0; nDstLen = 0; while (i < nSrcLen) { if (strncmp(pSrc, "= ", 3) == 0) // 软回车,跳过 { pSrc += 3; i += 3; } else { if (*pSrc == '=') // 是编码字节 { sscanf(pSrc, "=%02X", pDst); pDst++; pSrc += 3; i += 3; } else // 非编码字节 { *pDst++ = (unsigned char)*pSrc++; i++; } nDstLen++; } } // 输出加个结束符 *pDst = ''; return nDstLen; }

 
 

 

[相关资源]

rfc/STD文档: Inte .NET FAQ Archives bhw98的专栏: http://www.csdn.net/develop/author/netauthor/bhw98/

首次发布: 2003-06-23
最后修订: 2003-06-23

 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-956697/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-956697/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值