Last active
February 20, 2025 02:23
-
-
Save ryu1/8fa92a5ba681f2162733c54cb5803dbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts a string to a byte array using the UTF-8 encoding scheme, ensuring each byte fits in the signed range. | |
* In GraalJS, you cannot call java.lang.String#getBytes(). Use this instead. | |
* For details, see https://github.com/oracle/graaljs/issues/708 | |
* @param {string} str - The input string to be converted. | |
* | |
* @returns {number[]} - The byte array representation of the input string. | |
* | |
* @throws {Error} - If an error occurs during the conversion. | |
* | |
* @example | |
* const inputString = 'exampleText'; | |
* const byteArr = toSignedIntByteArray(inputString); | |
* console.log(byteArr); // Output: [101, 120, 97, 109, 112, 108, 101, 84, 101, 120, 116] | |
*/ | |
function toSignedIntByteArray(str) { | |
let byteArray = []; | |
for (const char of str) { | |
let codePoint = char.codePointAt(0); // Get the Unicode code point of the character | |
if (codePoint <= 0x7F) { | |
// 1 byte for code points in the range [0, 127] | |
byteArray.push(codePoint > 127 ? codePoint - 256 : codePoint); | |
} else if (codePoint <= 0x7FF) { | |
// 2 bytes for code points in the range [128, 2047] | |
byteArray.push(0xC0 | (codePoint >> 6)); | |
byteArray.push(0x80 | (codePoint & 0x3F)); | |
} else if (codePoint <= 0xFFFF) { | |
// 3 bytes for code points in the range [2048, 65535] | |
byteArray.push(0xE0 | (codePoint >> 12)); | |
byteArray.push(0x80 | ((codePoint >> 6) & 0x3F)); | |
byteArray.push(0x80 | (codePoint & 0x3F)); | |
} else if (codePoint <= 0x10FFFF) { | |
// 4 bytes for code points in the range [65536, 1114111] | |
byteArray.push(0xF0 | (codePoint >> 18)); | |
byteArray.push(0x80 | ((codePoint >> 12) & 0x3F)); | |
byteArray.push(0x80 | ((codePoint >> 6) & 0x3F)); | |
byteArray.push(0x80 | (codePoint & 0x3F)); | |
} | |
} | |
return byteArray.map(byte => (byte > 127 ? byte - 256 : byte)); // Ensure each byte fits in signed range | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ECMAScriptで、node.jsのAPIやTextEncoderに頼らず、文字列をbyte配列に変換する。