@root/encoding
Version:
Leightweight, Zero-dependency, translation between Unicode, Buffers, Base64, Hex, Binary Strings, UCS-2, UTF-8, etc.
41 lines (28 loc) • 583 B
JavaScript
;
var Enc = require('./bytes.js');
// to Hex
function bufToHex(buf) {
// in case it's a Uint8Array
return Buffer.from(buf).toString('hex');
}
Enc.bufToHex = bufToHex;
Enc.strToHex = function(str) {
return Buffer.from(str).toString('hex');
};
// from Hex
function hexToBuf(hex) {
return Buffer.from(hex, 'hex');
}
Enc.hexToBuf = hexToBuf;
Enc.hexToStr = function(hex) {
return hexToBuf(hex).toString('utf8');
};
// to/from num
Enc.numToHex = function(d) {
d = d.toString(16);
if (d.length % 2) {
return '0' + d;
}
return d;
};
module.exports = Enc;