Javascript中的Html编码和C中的解码#

Javascript中的Html编码和C中的解码#,javascript,asp.net-mvc-3,Javascript,Asp.net Mvc 3,我试图在js文件中对汉字进行html编码,但它返回了一些无效字符 My text : 奥妙 全自动洁彩深层洁净洗衣液3kg My html encode: kgoxAyM2tn My htmldecoded term: �1#6 我的代码: // TODO get the ID of the select so that we can add this as a new dependency var searchKeyword = $('#tx

我试图在js文件中对汉字进行html编码,但它返回了一些无效字符

My text :    奥妙 全自动洁彩深层洁净洗衣液3kg
My html encode:  kgoxAyM2tn
My htmldecoded term: �1#6
我的代码:

 // TODO get the ID of the select so that we can add this as a new dependency
                    var searchKeyword = $('#txtBox' + id).val();
                    var pId =  $('#SearchTerm_ > option:selected').attr("value");
                    var encodedSearchTerm = Encode(searchKeyword);
                    if (!(searchKeyword)) {
                        return false;
                    } else if (pId <= 0) {
                        return false;
                    }

     function Encode(urlFragment) {
     urlFragment = $.base64.encode(urlFragment);
     return urlFragment;
  };
//TODO获取select的ID,以便我们可以将其添加为新的依赖项
var searchKeyword=$('#txtBox'+id).val();
var pId=$('#SearchTerm_u>option:selected').attr(“值”);
var encodedSearchTerm=Encode(搜索关键字);
如果(!(搜索关键字)){
返回false;
}否则,如果(pId作为答案发布:

此jfiddle进行编码将使您到达需要的位置:

var Base64={
_keyStr:“ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789+/=”,
编码:功能(输入){
var输出=”;
变量chr1,chr2,chr3,enc1,enc2,enc3,enc4;
var i=0;
输入=Base64.\u utf8\u编码(输入);
while(i>2;
enc2=((chr1&3)>4);
enc3=((chr2&15)>6);
enc4=chr3&63;
if(isNaN(chr2)){
enc3=enc4=64;
}否则如果(isNaN(chr3)){
enc4=64;
}
输出=输出+此。\键字符(enc1)+此。\键字符(enc2)+此。\键字符(enc3)+此。\键字符(enc4);
}
返回输出;
},
解码:功能(输入){
var输出=”;
变种chr1,chr2,chr3;
变量enc1、enc2、enc3、enc4;
var i=0;
输入=输入。替换(/[^A-Za-z0-9\+\/\=]/g,”);
while(i4);
chr2=((enc2&15)>2);
chr3=((enc3&3)127)和&(c<2048)){
utftext+=String.fromCharCode((c>>6)| 192);
utftext+=String.fromCharCode((c&63)| 128);
}
否则{
utftext+=String.fromCharCode((c>>12)| 224);
utftext+=String.fromCharCode((c>>6)和63)| 128);
utftext+=String.fromCharCode((c&63)| 128);
}
}
返回utftext;
},
_utf8_解码:函数(utftext){
var字符串=”;
var i=0;
var c=c1=c2=0;
while(i191)和&(c<224)){
c2=utftext.charCodeAt(i+1);

string+=string.fromCharCode(((c&31)请尝试在此处使用这些函数。它们似乎与您的文本配合得很好。@better\u use\u mkstemp:这一个有效。请回答,以便我可以标记它。谢谢
var Base64 = {


_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",


encode: function(input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;

    input = Base64._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 + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

    }

    return output;
},


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 = this._keyStr.indexOf(input.charAt(i++));
        enc2 = this._keyStr.indexOf(input.charAt(i++));
        enc3 = this._keyStr.indexOf(input.charAt(i++));
        enc4 = this._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 = Base64._utf8_decode(output);

    return output;

},

_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;
},

_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;
}

}



var encode = document.getElementById('encode'),
decode = document.getElementById('decode'),
output = document.getElementById('output'),
input = document.getElementById('input');


encode.onclick = function() {
output.innerHTML = Base64.encode(input.value);
}

decode.onclick = function() {
var $str = output.innerHTML;
output.innerHTML = Base64.decode($str);
}