Fixed bug in numToBytes and util -> conv

This commit is contained in:
vub 2013-10-08 06:34:15 -04:00
commit a753f069a5
3 changed files with 12 additions and 3 deletions

View file

@ -26,9 +26,18 @@ module.exports = {
* Create a byte array representing a number with the given length
*/
numToBytes: function(num,bytes) {
if (bytes == 0 || (bytes === undefined && num === 0)) return [];
if (bytes === undefined) bytes = 8;
if (bytes == 0) return [];
else return [num % 256].concat(module.exports.numToBytes(Math.floor(num / 256),bytes-1));
},
/**
* Create a byte array representing a number with the given length
*/
bytesToNum: function(bytes) {
if (bytes.length == 0) return 0;
else return bytes[0] + 256 * bytesToNum(bytes.slice(1));
},
/**
* Turn an integer into a "var_int".
*