Nodejs Hex Encode hexEncode()

Here you can find the source of hexEncode()

Method Source Code

var string = "hello world"
var apple = "\uF8FF"

string.charCodeAt(0).toString(16);

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

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

Related

  1. hexEncode()
    String.prototype.hexEncode = function() {
      var hex, i;
      var result = "";
      for (i = 0; i < this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += ("000" + hex).slice(-4);
      return result;
    };
    ...
    
  2. hexEncode()
    String.prototype.hexEncode = function(){
        let hex, i
        let result = ""
        for (i=0; i<this.length; i++) {
            hex = this.charCodeAt(i).toString(16)
            result += (""+hex).slice(-4)
        return result
    
  3. hexEncode()
    String.prototype.hexEncode = function () {
        var result = '';
        var index = 0;
        var hex;
        while (index < this.length) {
            hex = this.charCodeAt(index++).toString(16);
            while (hex.length < 2) { hex = hex; }
            result += hex;
        return result;
    
  4. hexEncode8()
    String.prototype.hexEncode8 = function(){
        var hex, i;
        var result = "";
        for (i=0; i<this.length; i++) {
            hex = this.charCodeAt(i).toString(16);
            result += ("0"+hex).slice(-2);
        return result