谁能给我写个简单的汉字Base64解码代码并且告诉我原理。
发布网友
发布时间:2022-05-02 09:10
我来回答
共3个回答
热心网友
时间:2022-04-18 08:53
这是编解码的代码,确认有效。
static size_t _base64Decode(unsigned char* pszNormal, const unsigned char* pczBase64, int nSize)
{
ASSERT(nSize > ZERO);
unsigned char* pszBase64Copy = NULL;
if( pczBase64 == pszNormal )
{
pszBase64Copy = new unsigned char[nSize]; ASSERT(pszBase64Copy);
::memcpy(pszBase64Copy, pczBase64, nSize);
}
size_t nsize = 0;
unsigned char* pucOrig = (NULL == pszBase64Copy) ? (unsigned char*)pczBase64 : pszBase64Copy;
unsigned char* pucDest = pszNormal;
for(int nNow = 0; nNow < nSize - 4; nNow += 4)
{
unsigned long ul = *(unsigned long*)pucOrig;
register int b0 = (_getBase64Index((char)B0(ul))<<2|_getBase64Index((char)B1(ul))<<2>>6)&MAX_BYTE;
register int b1 = (_getBase64Index((char)B1(ul))<<4|_getBase64Index((char)B2(ul))<<2>>4)&MAX_BYTE;
register int b2 = (_getBase64Index((char)B2(ul))<<6|_getBase64Index((char)B3(ul))<<2>>2)&MAX_BYTE;
*((unsigned long*)pucDest) = b0|b1<<8|b2<<16;
pucOrig += 4;
pucDest += 3;
nsize += 3;
}
if( nNow < nSize )
{
int nRest = nSize - nNow;
unsigned long ul = 0;
for(int j = 0; j < nRest; ++j){
*(((unsigned char*)&ul)+j) = *pucOrig++;
}
register int b0 = (_getBase64Index((char)B0(ul))<<2|_getBase64Index((char)B1(ul))<<2>>6)&MAX_BYTE;
*pucDest++ = b0;
nsize++;
if( (_EQ_ != B1(ul)) && (_EQ_ != B2(ul)) )
{
register int b1 = (_getBase64Index((char)B1(ul))<<4|_getBase64Index((char)B2(ul))<<2>>4)&MAX_BYTE;
*pucDest++ = b1;
nsize++;
}
if( (_EQ_ != B2(ul)) && (_EQ_ != B3(ul)) )
{
register int b2 = (_getBase64Index((char)B2(ul))<<6|_getBase64Index((char)B3(ul))<<2>>2)&MAX_BYTE;
*pucDest++ = b2;
nsize++;
}
}
*pucDest = _EOS_;
if( pszBase64Copy )
{
delete[] pszBase64Copy;
pszBase64Copy = NULL;
}
return nsize;
}
static int _base64Encode(unsigned char* pszBase64, const unsigned char* pczNormal, int nSize)
{
ASSERT(nSize > ZERO);
unsigned char* pszNormalCopy = NULL;
if( pczNormal == pszBase64 )
{
pszNormalCopy = new unsigned char[nSize];
::memcpy(pszNormalCopy, pczNormal, nSize);
}
size_t nsize = 0;
unsigned char* pucOrig = (NULL == pszNormalCopy) ? (unsigned char*)pczNormal : pszNormalCopy;
unsigned char* puc64 = pszBase64;
for(int nNow = 0; nNow < nSize - 3; nNow += 3)
{
unsigned long ul = *(unsigned long*)pucOrig;
register int b0 = _getBase64Char((B0(ul) >> 2) & 0x3F);
register int b1 = _getBase64Char((B0(ul) << 6 >> 2 | B1(ul) >> 4) & 0x3F);
register int b2 = _getBase64Char((B1(ul) << 4 >> 2 | B2(ul) >> 6) & 0x3F);
register int b3 = _getBase64Char((B2(ul) << 2 >> 2) & 0x3F);
*((unsigned long*)puc64) = b0 | b1 << 8 | b2 << 16 | b3 << 24;
nsize += 4;
puc64 += 4;
pucOrig += 3;
}
if( nNow < nSize )
{
int nRest = nSize - nNow;
unsigned long ul = 0;
for(int j = 0; j < nRest; ++j){
*(((unsigned char*)&ul) + j) = *pucOrig++;
}
puc64[0] = _getBase64Char((B0(ul) >> 2) & 0x3F);
puc64[1] = _getBase64Char((B0(ul) << 6 >> 2 | B1(ul) >> 4) & 0x3F);
puc64[2] = (nRest > 1)?_getBase64Char((B1(ul) << 4 >> 2 | B2(ul) >> 6) & 0x3F) : _EQ_;
puc64[3] = (nRest > 2)?_getBase64Char((B2(ul) << 2 >> 2) & 0x3F) : _EQ_;
puc64 += 4;
nsize += 4;
}
*puc64 = _EOS_;
if( pszNormalCopy )
{
delete[] pszNormalCopy;
pszNormalCopy = NULL;
}
return nsize;
}
在网络传输的时候,需要使用多字节编码,才能得到有效的汉字。
也就是说,当你传输一个汉字的时候,应该先把它用多字节编码,然后在接收端再把它还原为之前的编码。
另外,能否编辑中文,与操作系统的字符集有关,如果是中文操作系统的话,默认的是中文字符集,这是可以修改的,如果你修改成其他西文字符集的话,对汉字的编辑可能就会出现乱码。
热心网友
时间:2022-04-18 10:11
首先你需要把汉字按Unicode编码输入(也就是wchar_t *的字符串而不是char *的),然后再转成utf-8编码。然后再转成base64...
鉴于你能写出base64编码,utf-8的规则只要上网搜一下就可以了
热心网友
时间:2022-04-18 11:46
不太明白,但是希望你能够早日得到解决。