hex.js
583 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
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;