base64解码图片,搞不定啊
用c#很容易
byte[] temp = Convert.FromBase64String(strbase);
File.WriteAllBytes(@"d:\d1.png", temp);
这样就好了,而且保存的文件也没有问题。
可以用vc就总是不行。网上找了2个算法,解码后两个值都不相同,跟c#解析出来的也不同。保存成文件后,能显示图片的大小,可是图片总是打不开。
两种算法,如下:
//Base64 解码
static char GetBase64Value(char ch) //得到编码值
{
if ((ch >= 'A') && (ch <= 'Z')) // A ~ Z
return ch - 'A';
if ((ch >= 'a') && (ch <= 'z')) // a ~ z
return ch - 'a' + 26;
if ((ch >= '0') && (ch <= '9')) // 0 ~ 9
return ch - '0' + 52;
switch (ch) // 其它字符
{
case '+':
return 62;
case '/':
return 63;
case '=': //Base64 填充字符
return 0;
default:
return 0;
}
}
// 解码函数
static int Base64Decode( char *OrgString, char *Base64String, int Base64StringLen, bool bForceDecode ) //解码函数
{
// OrgString 保存解码结果字符串指针
// Base64String 待解码字符串指针
// Base64StringLen 待解码字符串长度
// bForceDecode 当待解码字符串长度错误时,是否强制解码
// true 强制解码
// false 不强制解码
if( Base64StringLen % 4 && !bForceDecode ) //如果不是 4 的倍数,则 Base64 编码有问题
{
OrgString[0] = '\0';
return -1;
}
unsigned char Base64Encode[4];
int OrgStringLen=0;
while( Base64StringLen > 2 ) //当待解码个数不足3个时,将忽略它(强制解码时有效)
{
Base64Encode[0] = GetBase64Value(Base64String[0]);
Base64Encode[1] = GetBase64Value(Base64String[1]);
Base64Encode[2] = GetBase64Value(Base64String[2]);
Base64Encode[3] = GetBase64Value(Base64String[3]);
*OrgString ++ = (Base64Encode[0] << 2) | (Base64Encode[1] >> 4);
*OrgString ++ = (Base64Encode[1] << 4) | (Base64Encode[2] >> 2);
*OrgString ++ = (Base64Encode[2] << 6) | (Base64Encode[3]);
Base64String += 4;
Base64StringLen -= 4;
OrgStringLen += 3;
}
return OrgStringLen;
}
static long bBase64Decode( string &OrgString,const char* pSrc,int nSrcLen)
{
int nValue,i=0;
const char DeBase64Tab[]=
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, // '+'
0, 0, 0,
63, // '/'
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
0, 0, 0, 0, 0, 0,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
};
// 取4个字符,解码到一个长整数,再经过移位得到3个字节
while (i < nSrcLen)
{
if (*pSrc != '\r' && *pSrc!='\n')
{
nValue = DeBase64Tab[*pSrc++] << 18;
nValue += DeBase64Tab[*pSrc++] << 12;
OrgString += (nValue & 0x00ff0000) >> 16;
if (*pSrc != '=')
{
nValue += DeBase64Tab[*pSrc++] << 6;
OrgString += (nValue & 0x0000ff00) >> 8;
if (*pSrc != '=')
{
nValue += DeBase64Tab[*pSrc++];
OrgString +=nValue & 0x000000ff;
}
}
i += 4;
}
else // 回车换行,跳过
{
pSrc++;
i++;
}
}
return OrgString.length();
}
谢谢大家,着急