2021-02-11 06:12:41 +01:00
|
|
|
// @flow
|
2021-02-25 18:43:56 +01:00
|
|
|
|
2021-02-11 06:12:41 +01:00
|
|
|
export function toHex(str: string): string {
|
2021-02-25 18:43:56 +01:00
|
|
|
const array = Array.from(str);
|
|
|
|
|
|
|
|
let result = '';
|
|
|
|
|
|
|
|
for (var i = 0; i < array.length; i++) {
|
|
|
|
const val = array[i];
|
2021-03-02 22:22:25 +01:00
|
|
|
const utf = toUTF8Array(val)
|
|
|
|
.map((num) => num.toString(16))
|
|
|
|
.join('');
|
2021-02-25 18:43:56 +01:00
|
|
|
|
|
|
|
result += utf;
|
2021-02-11 06:12:41 +01:00
|
|
|
}
|
2021-02-25 18:43:56 +01:00
|
|
|
|
2021-02-11 06:12:41 +01:00
|
|
|
return result;
|
|
|
|
}
|
2021-02-25 18:43:56 +01:00
|
|
|
|
2021-03-02 22:22:25 +01:00
|
|
|
// https://gist.github.com/joni/3760795
|
|
|
|
// See comment that fixes an issue in the original gist
|
|
|
|
function toUTF8Array(str: string): Array<number> {
|
2021-02-25 18:43:56 +01:00
|
|
|
var utf8 = [];
|
|
|
|
for (var i = 0; i < str.length; i++) {
|
|
|
|
var charcode = str.charCodeAt(i);
|
|
|
|
if (charcode < 0x80) utf8.push(charcode);
|
|
|
|
else if (charcode < 0x800) {
|
|
|
|
utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f));
|
|
|
|
} else if (charcode < 0xd800 || charcode >= 0xe000) {
|
|
|
|
utf8.push(0xe0 | (charcode >> 12), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f));
|
|
|
|
}
|
|
|
|
// surrogate pair
|
|
|
|
else {
|
|
|
|
i++;
|
|
|
|
charcode = (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)) + 0x010000;
|
|
|
|
utf8.push(
|
|
|
|
0xf0 | (charcode >> 18),
|
|
|
|
0x80 | ((charcode >> 12) & 0x3f),
|
|
|
|
0x80 | ((charcode >> 6) & 0x3f),
|
|
|
|
0x80 | (charcode & 0x3f)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 22:22:25 +01:00
|
|
|
|
2021-02-25 18:43:56 +01:00
|
|
|
return utf8;
|
|
|
|
}
|