MCPcopy
hub / github.com/gchq/CyberChef / strToByteArray

Method strToByteArray

src/core/Utils.mjs:537–549  ·  view source on GitHub ↗

* Converts a string to a byte array. * Treats the string as UTF-8 if any values are over 255. * * @param {string} str * @returns {byteArray} * * @example * // returns [72,101,108,108,111] * Utils.strToByteArray("Hello"); * * // returns [228,189,160,2

(str)

Source from the content-addressed store, hash-verified

535 * Utils.strToByteArray("你好");
536 */
537 static strToByteArray(str) {
538 log.debug(`Converting string[${str?.length}] to byte array`);
539 if (!str) return [];
540 const byteArray = new Array(str.length);
541 let i = str.length, b;
542 while (i--) {
543 b = str.charCodeAt(i);
544 byteArray[i] = b;
545 // If any of the bytes are over 255, read as UTF-8
546 if (b > 255) return Utils.strToUtf8ByteArray(str);
547 }
548 return byteArray;
549 }
550
551
552 /**

Callers 13

convertToByteArrayMethod · 0.80
strToUtf8ByteArrayMethod · 0.80
salsa20BlockFunction · 0.80
hsalsa20Function · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
chachaFunction · 0.80
runMethod · 0.80
runMethod · 0.80

Calls 1

strToUtf8ByteArrayMethod · 0.80

Tested by

no test coverage detected