About
A byte array is any array of byte.
Library represents them generally as a multi-byte word with the possibility to change the endianess. See for instance, the Array Buffer in Javascript
Conversion
To Integer
To a integer data type
The algorithm is power based <MATH> \text{integer} = \text{byteN} . 256^{n-1} + \text{...} + \text{byte3} . 256 . 256 + \text{byte2} . 256 + \text{byte1} </MATH>
Example in Javascript with the function byteArrayToInteger that accepts an array of byte in string format or integer
let byteArrayToInteger = function(/*string[]|int[]*/byteArray) {
let value = 0;
let byteValue;
let byteString;
let byteInteger;
for ( let i = 0; i < byteArray.length; i++) {
byteValue = byteArray[byteArray.length-1-i];
if (typeof(byteValue) === 'string' || byteValue instanceof String){
byteInteger = parseInt(byteValue,2);
} else {
byteInteger = byteValue;
}
value = byteInteger * Math.pow(256,i) + value;
}
return value;
};
- Usage
let byteArray = ['00000001','11111111'];
/* As a byte is an integer from 0 to 255 in decimal, same as */
byteArray = [1,255];
console.log(byteArrayToInteger(byteArray)+" (ie 256+255)");
/** others **/
console.log(byteArrayToInteger(['10110000','11110011'])+" (ie 256*176+243)")
- Output: