當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
VC\JS Base64转码
生活随笔
收集整理的這篇文章主要介紹了
VC\JS Base64转码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在使用fireBreath開(kāi)發(fā)跨瀏覽器插件的過(guò)程中,遇到插件接口參數(shù)或返回值中文亂碼的問(wèn)題,于是想起采用Base64先編碼,到插件函數(shù)內(nèi)再解碼的方法。
亂碼原因:
1、VC源文件默認(rèn)是ANSI編碼,API轉(zhuǎn)換時(shí)跟源文件使用的編碼有關(guān)系
一、 vc++類
//------------------------------------------------------------------------ //base64.h //------------------------------------------------------------------------ #ifndef ___BASE64_H___ #define ___BASE64_H___#include <string>using namespace std;class CBase64 { public:static bool Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut);static bool Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen);static bool Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen) ;static string Encode(string data); static string Decode(string data);static int GetDecodeBufferLength(string data);static string GBToUTF8(const char*); //將gb2312編碼轉(zhuǎn)為UTF-8字符static string UTF8ToGB(const char*); //將UTF-8編碼轉(zhuǎn)為gb2312字符 };#endif // ___BASE64_H___ //----------------------------------------------------------------------------- //base64.cpp //----------------------------------------------------------------------------- //#include "stdafx.h" #include "Base64.h" #include static const char *g_pCodes ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";static const unsigned char g_pMap[256] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,255, 254, 255, 255, 255, 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, 255, 255, 255, 255, 255,255, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255 };bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen) {unsigned long i, len2, leven;unsigned char *p;if(pOut == NULL || *uOutLen == 0)return false;//ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL));len2 = ((uInLen + 2) / 3) << 2;if((*uOutLen) < (len2 + 1)) return false;p = pOut;leven = 3 * (uInLen / 3);for(i = 0; i < leven; i += 3){*p++ = g_pCodes[pIn[0] >> 2];*p++ = g_pCodes[((pIn[0] & 3) << 4) + (pIn[1] >> 4)];*p++ = g_pCodes[((pIn[1] & 0xf) << 2) + (pIn[2] >> 6)];*p++ = g_pCodes[pIn[2] & 0x3f];pIn += 3;}if (i < uInLen){unsigned char a = pIn[0];unsigned char b = ((i + 1) < uInLen) ? pIn[1] : 0;unsigned char c = 0;*p++ = g_pCodes[a >> 2];*p++ = g_pCodes[((a & 3) << 4) + (b >> 4)];*p++ = ((i + 1) < uInLen) ? g_pCodes[((b & 0xf) << 2) + (c >> 6)] : '=';*p++ = '=';}*p = 0; // Append NULL byte*uOutLen = p - pOut;return true; }bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut) {unsigned long i, len2, leven;strOut = "";//ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL));len2 = ((uInLen + 2) / 3) << 2;//if((*uOutLen) < (len2 + 1)) return false;//p = pOut;leven = 3 * (uInLen / 3);for(i = 0; i < leven; i += 3){strOut += g_pCodes[pIn[0] >> 2];strOut += g_pCodes[((pIn[0] & 3) << 4) + (pIn[1] >> 4)];strOut += g_pCodes[((pIn[1] & 0xf) << 2) + (pIn[2] >> 6)];strOut += g_pCodes[pIn[2] & 0x3f];pIn += 3;}if (i < uInLen){unsigned char a = pIn[0];unsigned char b = ((i + 1) < uInLen) ? pIn[1] : 0;unsigned char c = 0;strOut += g_pCodes[a >> 2];strOut += g_pCodes[((a & 3) << 4) + (b >> 4)];strOut += ((i + 1) < uInLen) ? g_pCodes[((b & 0xf) << 2) + (c >> 6)] : '=';strOut += '=';}//*p = 0; // Append NULL byte//*uOutLen = p - pOut;return true; }bool CBase64::Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen) {unsigned long t, x, y, z;unsigned char c;unsigned long g = 3;//ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL));for(x = y = z = t = 0; x < strIn.length(); x++){c = g_pMap[strIn[x]];if(c == 255) continue;if(c == 254) { c = 0; g--; }t = (t << 6) | c;if(++y == 4){if((z + g) > *uOutLen) { return false; } // Buffer overflowpOut[z++] = (unsigned char)((t>>16)&255);if(g > 1) pOut[z++] = (unsigned char)((t>>8)&255);if(g > 2) pOut[z++] = (unsigned char)(t&255);y = t = 0;}}*uOutLen = z;return true; }string CBase64::Encode(string data) {string s;Encode((const unsigned char*)(data.c_str()),(unsigned long)data.length(),s);return s; }string CBase64::Decode(string data) {string s;unsigned char* bytBuf = NULL;unsigned long len = 0;char* pBuf = NULL;string sRet;len = (data.length() >> 2) * 3;bytBuf = new unsigned char[len];memset(bytBuf,0,len);Decode(data,bytBuf,&len);pBuf = new char[len+1];memset(pBuf,0,len+1);memcpy(pBuf,bytBuf,len);sRet = pBuf;delete[] bytBuf; bytBuf = NULL;delete[] pBuf; pBuf = NULL;return sRet; }int CBase64::GetDecodeBufferLength(string data) {int len = 0;len = (data.length() >> 2) * 3-2;return len; }string CBase64::GBToUTF8(const char* str) {std::string result;WCHAR *strSrc;TCHAR *szRes;//獲得臨時(shí)變量的大小int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);strSrc = new WCHAR[i+1];MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i);//獲得臨時(shí)變量的大小i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);szRes = new TCHAR[i+1];int j=WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, (LPSTR)(LPCTSTR)szRes, i, NULL, NULL);result = (char*)szRes;delete []strSrc;delete []szRes;return result; }string CBase64::UTF8ToGB(const char* str) {std::string result;WCHAR *strSrc;TCHAR *szRes;//獲得臨時(shí)變量的大小int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);strSrc = new WCHAR[i+1];MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);//獲得臨時(shí)變量的大小i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);szRes = new TCHAR[i+1];WideCharToMultiByte(CP_ACP, 0, strSrc, -1, (LPSTR)(LPCTSTR)szRes, i, NULL, NULL);result = (char*)szRes;delete []strSrc;delete []szRes;return result; }二 、js類
<script type="text/javascript"> /** * * Base64 encode / decode * * @author haitao.tu * @date 2010-04-26 * @email tuhaitao@foxmail.com * */function Base64() {// private property_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";// public method for encodingthis.encode = function (input) {var output = "";var chr1, chr2, chr3, enc1, enc2, enc3, enc4;var i = 0;input = _utf8_encode(input);while (i < input.length) {chr1 = input.charCodeAt(i++);chr2 = input.charCodeAt(i++);chr3 = input.charCodeAt(i++);enc1 = chr1 >> 2;enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);enc4 = chr3 & 63;if (isNaN(chr2)) {enc3 = enc4 = 64;} else if (isNaN(chr3)) {enc4 = 64;}output = output +_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +_keyStr.charAt(enc3) + _keyStr.charAt(enc4);}return output;}// public method for decodingthis.decode = function (input) {var output = "";var chr1, chr2, chr3;var enc1, enc2, enc3, enc4;var i = 0;input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");while (i < input.length) {enc1 = _keyStr.indexOf(input.charAt(i++));enc2 = _keyStr.indexOf(input.charAt(i++));enc3 = _keyStr.indexOf(input.charAt(i++));enc4 = _keyStr.indexOf(input.charAt(i++));chr1 = (enc1 << 2) | (enc2 >> 4);chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);chr3 = ((enc3 & 3) << 6) | enc4;output = output + String.fromCharCode(chr1);if (enc3 != 64) {output = output + String.fromCharCode(chr2);}if (enc4 != 64) {output = output + String.fromCharCode(chr3);}}output = _utf8_decode(output);return output;}// private method for UTF-8 encoding_utf8_encode = function (string) {string = string.replace(/\r\n/g,"\n");var utftext = "";for (var n = 0; n < string.length; n++) {var c = string.charCodeAt(n);if (c < 128) {utftext += String.fromCharCode(c);} else if((c > 127) && (c < 2048)) {utftext += String.fromCharCode((c >> 6) | 192);utftext += String.fromCharCode((c & 63) | 128);} else {utftext += String.fromCharCode((c >> 12) | 224);utftext += String.fromCharCode(((c >> 6) & 63) | 128);utftext += String.fromCharCode((c & 63) | 128);}}return utftext;}// private method for UTF-8 decoding_utf8_decode = function (utftext) {var string = "";var i = 0;var c = c1 = c2 = 0;while ( i < utftext.length ) {c = utftext.charCodeAt(i);if (c < 128) {string += String.fromCharCode(c);i++;} else if((c > 191) && (c < 224)) {c2 = utftext.charCodeAt(i+1);string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));i += 2;} else {c2 = utftext.charCodeAt(i+1);c3 = utftext.charCodeAt(i+2);string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));i += 3;}}return string;} } </script>
js
var b = new Base64();var dnName = "CN=寧子,O=電子公司,OU=研究部,L=鄭州市,S=河南省,C=CN,E=test@163.net";var s = b.encode(dnName);alert(s);s1 = b.decode(s); alert(s1);vc
//參數(shù):以base64編碼的字符串傳進(jìn)去 std::string Func(const std::string dnName) {int nRet =0;char buf[2048] = {0};int len = 2048;std::string strGB2312DbName = CBase64::Decode(dnName);//解碼std::string strDnName = CBase64::UTF8ToGB(strGB2312DbName.c_str());//utf8->GB2312 }源文件下載:http://download.csdn.net/detail/jiftlixu/6019275
總結(jié)
以上是生活随笔為你收集整理的VC\JS Base64转码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Unicode与UTF-8互转(C语言实
- 下一篇: C# 导出EXCEL文件