bytes.js
922 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
41
42
43
44
45
46
47
48
49
50
'use strict';
var Enc = module.exports;
/*
Enc.bufToUint8 = function bufToUint8(buf) {
return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
};
*/
// from Binary Strings
Enc.binToBuf = function(bin) {
return Buffer.from(bin, 'binary');
};
Enc.binToStr = function(bin) {
return Enc.binToBuf(bin).toString('utf8');
};
// from Buffer
Enc.bufToBin = function(buf) {
return Buffer.from(buf).toString('binary');
};
Enc.bufToStr = function(buf) {
return Buffer.from(buf).toString('utf8');
};
// from Unicode Strings
Enc.strToBin = function(str) {
return Buffer.from(str).toString('binary');
};
Enc.strToBuf = function(str) {
// default is 'utf8'
return Buffer.from(str);
};
// Base64 and Hex
Enc.base64ToHex = function(b64) {
return Buffer.from(b64, 'base64').toString('hex');
};
Enc.hexToBase64 = function(hex) {
return Buffer.from(hex, 'hex').toString('base64');
};