Javascript String hexEncode()

Description

Javascript String hexEncode()


String.prototype.hexEncode = function() {
  var hex, i;//from   ww w.ja v a 2 s.  c  o  m

  var result = "";
  for (i = 0; i < this.length; i++) {
    hex = this.charCodeAt(i).toString(16);
    result += ("000" + hex).slice(-4);
  }

  return result;
};

String.prototype.hexDecode = function() {
  var j;
  var hexes = this.match(/.{1,4}/g) || [];
  var back = "";
  for (j = 0; j < hexes.length; j++) {
    back += String.fromCharCode(parseInt(hexes[j], 16));
  }

  return back;
};

function unicode2chinese(str) {
  return unescape(str.replace(/\\u/g, '%u'));
}

var str = "\u6f22\u5b57"; // "\u6f22\u5b57" === "??"
alert(str.hexEncode().hexDecode());
alert(unicode2chinese(str));

Javascript String hexEncode()

//encode string to hex
String.prototype.hexEncode = function(){
    var hex, i;//from   www.  j  a va 2 s  .  c  o  m

    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += ("000"+hex).slice(-4);
    }

    return result
}
// hex to string
String.prototype.hexDecode = function(){
    var j;
    var hexes = this.match(/.{1,4}/g) || [];
    var back = "";
    for(j = 0; j<hexes.length; j++) {
        back += String.fromCharCode(parseInt(hexes[j], 16));
    }

    return back;
}

var str = "\u6f22\u5b57"; // "\u6f22\u5b57" === "??"
console.log(str);

Javascript String hexEncode()

String.prototype.hexEncode = function(){
    var hex, i;/*from   w w  w  .j  a va  2  s  .c o m*/

    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += ("000"+hex).slice(-4);
    }

    return result
};

Javascript String hexEncode()

String.prototype.hexEncode = function(){
    var hex, i;//from   ww w  .j av a 2s  .  c  o  m
    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += ("000"+hex).slice(-4);
    }
    return result;
};
String.prototype.hexDecode = function(){
    var j;
    var hexes = this.match(/.{1,4}/g) || [];
    var back = "";
    for(j = 0; j<hexes.length; j++) {
        back += String.fromCharCode(parseInt(hexes[j], 16));
    }
    return back;
};


surrogateToCodePoint = function(h, l) {
    return (h - 0xD800) * 0x400 + l - 0xDC00 + 0x10000;
};

codePointToSurrogate = function(c) {
    var h = Math.floor((c - 0x10000) / 0x400) + 0xD800;
    var l = (c - 0x10000) % 0x400 + 0xDC00;
    return "&#x" + h.toString(16).toUpperCase() + ";&#x" + l.toString(16).toUpperCase() + ";";
};

Javascript String hexEncode()

String.prototype.hexEncode = function(){
    var hex, i;/*from   ww w  . j a  v  a2  s.  c o  m*/

    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += ("000"+hex).slice(-4);
    }

    return result
}

String.prototype.hexEncode2 = function(){
    var hex, i;

    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += "\\u" + ("000"+hex).slice(-4);
    }

    return result
}

String.prototype.hexDecode = function(){
    var j;
    var hexes = this.match(/.{1,4}/g) || [];
    var back = "";
    for(j = 0; j<hexes.length; j++) {
        back += String.fromCharCode(parseInt(hexes[j], 16));
    }

    return back;
}



PreviousNext

Related