急求对字符串进行BASE64编码的代码~~~~~~

depotmen 2002-05-13 10:25:00
谢谢!!
...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kevin 2002-05-13
  • 打赏
  • 举报
回复
/* Convert BASE64 contents to binary
* Accepts: source
* length of source
* pointer to return destination length
* Returns: destination as binary or NIL if error
*/
//解码
void *rfc822_base64 (unsigned char *src,unsigned long srcl,unsigned long *len)
{
char c;
void *ret = malloc ((size_t) (*len = 4 + ((srcl * 3) / 4)));
char *d = (char *) ret;
int e = 0;
memset (ret,0,(size_t) *len); /* initialize block */
*len = 0; /* in case we return an error */
while (srcl--)
{ /* until run out of characters */
c = *src++; /* simple-minded decode */
if (isupper (c)) c -= 'A';
else if (islower (c)) c -= 'a' - 26;
else if (isdigit (c)) c -= '0' - 52;
else if (c == '+') c = 62;
else if (c == '/') c = 63;
else if (c == '=')
{ /* padding */
switch (e++)
{ /* check quantum position */
case 3:
e = 0; /* restart quantum */
break;
case 2:
if (*src == '=') break;
default: /* impossible quantum position */
free (ret);
return NIL;
}
continue;
}
else continue; /* junk character */
switch (e++)
{ /* install based on quantum position */
case 0:
*d = c << 2; /* byte 1: high 6 bits */
break;
case 1:
*d++ |= c >> 4; /* byte 1: low 2 bits */
*d = c << 4; /* byte 2: high 4 bits */
break;
case 2:
*d++ |= c >> 2; /* byte 2: low 4 bits */
*d = c << 6; /* byte 3: high 2 bits */
break;
case 3:
*d++ |= c; /* byte 3: low 6 bits */
e = 0; /* reinitialize mechanism */
break;
}
}
*len = d - (char *) ret; /* calculate data length */
return ret; /* return the string */
}

/* Convert binary contents to BASE64
* Accepts: source
* length of source
* pointer to return destination length
* Returns: destination as BASE64
*/
//编码
unsigned char *rfc822_binary (void *src,unsigned long srcl,unsigned long *len)
{
unsigned char *ret,*d;
unsigned char *s = (unsigned char *) src;
char *v = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned long i = ((srcl + 2) / 3) * 4;
*len = i += 2 * ((i / 60) + 1);
d = ret = (unsigned char *) malloc ((size_t) ++i);
for (i = 0; srcl; s += 3)
{ /* process tuplets */
*d++ = v[s[0] >> 2]; /* byte 1: high 6 bits (1) */
/* byte 2: low 2 bits (1), high 4 bits (2) */
*d++ = v[((s[0] << 4) + (--srcl ? (s[1] >> 4) : 0)) & 0x3f];
/* byte 3: low 4 bits (2), high 2 bits (3) */
*d++ = srcl ? v[((s[1] << 2) + (--srcl ? (s[2] >> 6) : 0)) & 0x3f] : '=';
/* byte 4: low 6 bits (3) */
*d++ = srcl ? v[s[2] & 0x3f] : '=';
if (srcl)
srcl--; /* count third character if processed */
if ((++i) == 15)
{ /* output 60 characters? */
i = 0; /* restart line break count, insert CRLF */
*d++ = '\015'; *d++ = '\012';
}
}
*d++ = '\015'; *d++ = '\012'; /* insert final CRLF */
*d = '\0'; /* tie off string */
if (((unsigned long) (d - ret)) != *len)
printf ("rfc822_binary logic flaw");
return ret; /* return the resulting string */
}
yinx 2002-05-13
  • 打赏
  • 举报
回复
'下面我给出Base64的编码和解码的C语言描述:
'/*Base64编码*/
'void Base64(unsigned char chasc[3],unsigned char chuue[4])
'/*
'chasc: 未编码的二进制代码
'chuue: 编码过的Base64代码
'*/
'{
'int i,k=2;
'unsinged char t=NULL;
'for(i=0;i<3;i++)
'{
' *(chuue+i)=*(chasc+i)>>k;
' *(chuue+i)|=t;
' t=*(chasc+i)<<(8-k);
' t>>=2;
' k+=2;
'}
'*(chuue+3)=*(chasc+2)&63;
'
'for(i=0;i<4;i++)
' if((*(chuue+i)>=0)&&(*(chuue+i)<=25)) *(chuue+i)+=65;
' else if((*(chuue+i)>=26)&&(*(chuue+i)<=51)) *(chuue+i)+=71;
' else if((*(chuue+i)>=52)&&(*(chuue+i)<=61)) *(chuue+i)-=4;
' else if(*(chuue+i)==62) *(chuue+i)=43;
' else if(*(chuue+i)==63) *(chuue+i)=47;
'
'}
'/*Base64解码*/
'void unBase64(unsigned char chuue[4],unsigned char chasc[3])
'/*
'chuue: 未解码的Base64代码
'chasc: 解码过的二进制代码
'*/
'{int i,k=2;
'unsigned char t=NULL;
'
'for(i=0;i<4;i++)
' if((*(chuue+i)>=65)&&(*(chuue+i)<=90)) *(chuue+i)-=65;
' else if((*(chuue+i)>=97)&&(*(chuue+i)<=122)) *(chuue+i)-=71;
' else if((*(chuue+i)>=48)&&(*(chuue+i)<=57)) *(chuue+i)+=4;
' else if(*(chuue+i)==43) *(chuue+i)=62;
' else if(*(chuue+i)==47) *(chuue+i)=63;
' else if(*(chuue+i)==61) *(chuue+i)=0;
'
'for(i=0;i<3;i++)
'{*(chhex+i)=*(chuue+i)<<k;
' k+=2;
' t=*(chuue+i+1)>>8-k;
' *(chhex+i)|=t;
'}
'}
JeasonZhao 2002-05-13
  • 打赏
  • 举报
回复
邮件短消息方式告诉我,mail你一份

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧