Nibbles out of a Byte
Supporting the question found here: http://stackoverflow.com/questions/3756880/best-way-to-get-two-nibbles-out-of-a-byte-in-javascript
by vol7ron
HTML
<div id="output"></div>
CSS
#output { border : 1px solid #ccc ;
font-family : Courier New, Georgia, Verdana, Arial;
font-size : 12px ;
padding : 1em ;
}
.key { display : inline-block ;
width : 10em ;
}
.val { display : inline-block ;
margin-left : 2em ;
width : 5em ;
}
JavaScript
/* Prototypes */
Number.prototype.fromCharCode = function () {return String.fromCharCode(this); };
String.prototype.byte = function (val){ var a = new Array();
for(var i=(val||0),n=val===0?0:this.length-1; i<=n; i++)
a.push(this.charCodeAt(i) & 0xFF);
return a;
};
String.prototype.HiNibble = function (val){ var b = this.byte(val);
var a = new Array();
for(var i=0,n=b.length-1; i<=n; i++)
a.push(b[i] >> 4);
return a;
};
String.prototype.LoNibble = function (val){ var b = this.byte(val);
var a = new Array();
for(var i=0,n=b.length-1; i<=n; i++)
a.push(b[i] & 0xF);
return a;
};
/* Testing - Preparation */
function repeat(str, n) {
if (n && n < 1) return '';
var result = '';
while (n) {
result += str;
n--;
}
return result;
}
function bin(parm) {
switch (typeof parm){
case "object":
var a = parm;
var result = [];
var b = 0;
for (var n = a.length; n--;){
b = (a[n]*1).toString(2);
result.unshift(repeat('0',(8-b.length)) + b);
}
return result.join(",");
break;
case...