sniffypedia
Version:
An open, collaborative lookup of radio-identifiable (sniffable) things, represented as structured data. We believe in an open Internet of Things.
35 lines (28 loc) • 1.02 kB
JavaScript
/**
* Copyright reelyActive 2016-2021
* We believe in an open Internet of Things
*/
/**
* Convert the given data to a hexadecimal string of the given number of bytes.
* @param {Object} data The data to convert (Number, String or Buffer).
* @param {Number} length The target length of the string in bytes.
* @param {boolean} reverseEndianness Optionally reverse the endianness.
* @return {String} The data as a hexadecimal string.
*/
function convertToHexString(data, length, reverseEndianness) {
let hexString = null;
if(Number.isInteger(data) && (data >= 0)) {
hexString = data.toString(16).padStart(length * 2, '0');
}
if(Buffer.isBuffer(data)) {
hexString = data.toString('hex').padStart(length * 2, '0');
}
if(typeof data === 'string') {
hexString = data.padStart(length * 2, '0');
}
if(hexString && (reverseEndianness === true)) {
return hexString.match(/.{2}/g).reverse().join('');
}
return hexString;
}
module.exports.convertToHexString = convertToHexString;