Unpack a int in char format with a len of 4 - Node.js String

Node.js examples for String:Char

Description

Unpack a int in char format with a len of 4

Demo Code

/**/*  www .j a va2s .  co m*/
 * Unpack a int in char format with a len of 4
 * if is a non ascii value we show a dot
 * 
 * Parameters:
 * (int) n - the number to unpack
 * 
 * Returns:
 * (string) string of length 4
 */
function intToChars(n,type){
  var r = "";
  if (type==undefined) {
    type=4;
  }
  for(var i=0; i < type; ++i){
    var c = ((n>>(8*i)) & 0xff);
    if (32 <= c && c <= 126 && c!= 32){
      r += $('<div />').text(String.fromCharCode(c)).html();
    }else if (c == 32){
      r += "&nbsp;";
    }else{
      r += '.';
    }
  }
  return r;
}

Related Tutorials