react-qrcode-component
Version:
react qrcode qr.js
33 lines (32 loc) • 1.48 kB
JavaScript
// Convert from UTF-16, forcing the use of byte-mode encoding in our QR Code.
// This allows us to encode Hanji, Kanji, emoji, etc. Ideally we'd do more
// detection and not resort to byte-mode if possible, but we're trading off
// a smaller library for a smaller amount of data we can potentially encode.
// Based on http://jonisalonen.com/2012/from-utf-16-to-utf-8-in-javascript/
export function convertStr(str) {
let out = '';
for (let i = 0; i < str.length; i++) {
let charcode = str.charCodeAt(i);
if (charcode < 0x0080) {
out += String.fromCharCode(charcode);
} else if (charcode < 0x0800) {
out += String.fromCharCode(0xc0 | (charcode >> 6));
out += String.fromCharCode(0x80 | (charcode & 0x3f));
} else if (charcode < 0xd800 || charcode >= 0xe000) {
out += String.fromCharCode(0xe0 | (charcode >> 12));
out += String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f));
out += String.fromCharCode(0x80 | (charcode & 0x3f));
} else {
// This is a surrogate pair, so we'll reconsitute the pieces and work
// from that
i++;
charcode =
0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
out += String.fromCharCode(0xf0 | (charcode >> 18));
out += String.fromCharCode(0x80 | ((charcode >> 12) & 0x3f));
out += String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f));
out += String.fromCharCode(0x80 | (charcode & 0x3f));
}
}
return out;
}