当前位置:首页 > 新闻动态 > 网站文章

发个简单的编码程序千万不能错过

来源: 浏览:124 时间:2023-08-09

用过SMTP的应该都知道BASE64吧……

以下就是ASCII字符串转BASE64编码的小程序。

BASE64很简单,所以就不多说,程序可以更短,但是感觉这样写运算起来更快,长就长一点,将就看看吧。

#include

#include

#include

char* const pBASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

void ASCtoBASE64(char* &p)

{

int iLenth = strlen(p),j = 0;

char* tmp = (char*)malloc(2* iLenth);

memset(tmp,0,2*iLenth);

for(int i=0; i < iLenth-2; i += 3,j += 4)

{

int n = (p[i]<<16) + (p[i+1]<<8) + p[i+2];

int m = (n << 6) & 0xff000000;

n &= 0x3ffff;

m |= (n << 4) & 0x00ff0000;

n &= 0xfff;

m |= (n << 2) & 0x0000ff00;

n &= 0x3f;

m |= n;

tmp[j+3] = pBASE64[m & 255];

tmp[j+2] = pBASE64[(m>>= 8) & 255];

tmp[j+1] = pBASE64[(m>>=8) & 255];

tmp[j] = pBASE64[m>>=8];

}

if(i != iLenth)

{

int n = (iLenth-i) == 1 ? (p[i] << 16) : (p[i] << 16) + (p[i+1] << 8);

int m = (n << 6) & 0xff000000;

n &= 0x3ffff;

m |= (n << 4) & 0x00ff0000;

n &= 0xfff;

m |= (n << 2) & 0x0000ff00;

n &= 0x3f;

m |= n;

if(iLenth - i == 1)

{

tmp[j+1] = pBASE64[(m >>= 16) & 255];

tmp[j] = pBASE64[m >>=8];

strcat(tmp,"==");

}

else

{

tmp[j+2] = pBASE64[(m>>= 8) & 255];

tmp[j+1] = pBASE64[(m>>=8) & 255];

tmp[j] = pBASE64[m>>=8];

strcat(tmp,"=");

}

}

p = tmp;

}

int main()

{

char *a = "What a sb!";

char *b = "wysaid";

ASCtoBASE64(a);

ASCtoBASE64(b);

printf("%s %s ",a,b);

free(a);

free(b);

return 0;

}

忘记vc6上有个bug,for(int xx...)这样的,xx在块的外部,所以正确情况必须这样写:(看来该早点把vc6.0丢掉了,不过vs2010启动太慢,所以很多时候写短的代码时还是喜欢用vc6.0)

#include

#include

#include

char* const pBASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

void ASCtoBASE64(char* &p)

{

int iLenth = strlen(p),i=0,j = 0;

char* tmp = (char*)malloc(2* iLenth);

memset(tmp,0,2*iLenth);

for(; i < iLenth-2; i += 3,j += 4)

{

int n = (p[i]<<16) + (p[i+1]<<8) + p[i+2];

int m = (n << 6) & 0xff000000;

n &= 0x3ffff;

m |= (n << 4) & 0x00ff0000;

n &= 0xfff;

m |= (n << 2) & 0x0000ff00;

n &= 0x3f;

m |= n;

tmp[j+3] = pBASE64[m & 255];

tmp[j+2] = pBASE64[(m>>= 8) & 255];

tmp[j+1] = pBASE64[(m>>=8) & 255];

tmp[j] = pBASE64[m>>=8];

}

if(i != iLenth)

{

int n = (iLenth-i) == 1 ? (p[i] << 16) : (p[i] << 16) + (p[i+1] << 8);

int m = (n << 6) & 0xff000000;

n &= 0x3ffff;

m |= (n << 4) & 0x00ff0000;

n &= 0xfff;

m |= (n << 2) & 0x0000ff00;

n &= 0x3f;

m |= n;

if(iLenth - i == 1)

{

tmp[j+1] = pBASE64[(m >>= 16) & 255];

tmp[j] = pBASE64[m >>=8];

strcat(tmp,"==");

}

else

{

tmp[j+2] = pBASE64[(m>>= 8) & 255];

tmp[j+1] = pBASE64[(m>>=8) & 255];

tmp[j] = pBASE64[m>>=8];

strcat(tmp,"=");

}

}

p = tmp;

}

int main()

{

char *a = "What a sb!";

char *b = "wysaid";

ASCtoBASE64(a);

ASCtoBASE64(b);

printf("%s %s ",a,b);

free(a);

free(b);

return 0;

}

C/C++从入门到大牛369203660

以上就是所有的代码拉,如果你觉得还不错的话想跟我们一起学习不妨加群C/C++从入门到大牛369203660 千人大群有你想要有你所想。

请养成良好的阅读习惯,看完如果觉得喜欢的话请关注转发评论收藏一下 感谢!

地址 · ADDRESS

地址:建邺区新城科技园嘉陵江东街18号2层

邮箱:309474043@qq.Com

点击查看更多案例

联系 · CALL TEL

400-8793-956

售后专线:025-65016872

业务QQ:309474043    售后QQ:1850555641

©南京安优网络科技有限公司 版权所有   苏ICP备12071769号-4  网站地图