现有一个base64字符串,由于实际需要,需将其转换为BYTE[]数组,在网上看了下相关的转换方法,但是都无法实现,请教高手帮忙 ,给出相关代码是最好的,谢了。
base64字符串 byte[] 格式转换
------解决方案--------------------------------------------------------
const char _base64_encode_chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string decode(const std::string in_str)
{
std::string out_str;
char c1, c2, c3, c4;
int i = 0;
int len = in_str.length();
while ( i<len)
{
// read the first byte
do {
c1 = _base64_decode_chars[ in_str[i++] ];
} while ( i<len && c1==-1);
if ( c1==-1)
break;
// read the second byte
do {
c2 = _base64_decode_chars[ in_str[i++] ];
} while ( i<len && c2==-1);
if ( c2==-1 )
break;
// assamble the first byte
out_str += char( (c1<<2)
------解决方案--------------------------------------------------------
((c2&0x30)>>4) );
// read the third byte
do {
c3 = in_str[i++];
if ( c3==61 ) // meet with "=", break
return out_str;
c3 = _base64_decode_chars[ c3 ];
} while ( i<len && c3==-1);
if ( c3==-1 )
break;
// assamble the second byte
out_str += char( ((c2&0XF)<<4)
------解决方案--------------------------------------------------------
((c3&0x3C)>>2) );
// read the fourth byte
do {
c4 = in_str[i++];