-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
78 lines (67 loc) · 1.67 KB
/
utils.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
iZ³ | Izzzio blockchain - https://izzz.io
@author: Andrey Nedobylsky (admin@twister-vl.ru)
*/
module.exports = {
/**
* Convert hex number string to utf-16
* @param str
* @return {*}
*/
hexString2Unicode: function (str) {
if(str.length % 4 !== 0) {
return false;
}
str = str.toLowerCase();
let code = '';
str = str.match(/.{1,4}/g);
for (let s of str) {
if(s.length === 4) {
code += String.fromCharCode(parseInt(s, 16));
} else {
code += s;
}
}
return code;
},
/**
* Convert utf-16 string to hex
* @param uniStr
* @return {string}
*/
unicode2HexString: function (uniStr) {
let str = '';
for (let i = 0; i < uniStr.length; i++) {
let charCode = uniStr.charCodeAt(i);
if(charCode < 0x1000) {
str += '0';
}
if(charCode < 0x100) {
str += '0';
}
if(charCode < 0x10) {
str += '0';
}
str += charCode.toString(16);
}
return str;
},
/**
* Hex string to Uint8Array
* @param hexString
* @return {Uint8Array}
* @constructor
*/
hexString2Uint8Array: function (hexString) {
return new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)))
},
/**
* Uint8Array to hex string
* @param arr
* @return {*}
* @constructor
*/
uint8ArrayToHexString: function (arr) {
return String(Buffer.from(arr).toString('hex'));
},
};