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

Method byteArrayToUtf8

src/core/Utils.mjs:631–653  ·  view source on GitHub ↗

* Attempts to convert a byte array to a UTF-8 string. * * @param {byteArray|Uint8Array} byteArray * @returns {string} * * @example * // returns "Hello" * Utils.byteArrayToUtf8([72,101,108,108,111]); * * // returns "你好" * Utils.byteArrayToUtf8([228,18

(byteArray)

Source from the content-addressed store, hash-verified

629 * Utils.byteArrayToUtf8([228,189,160,229,165,189]);
630 */
631 static byteArrayToUtf8(byteArray) {
632 log.debug(`Converting byte array[${byteArray?.length}] to UTF8`);
633 if (!byteArray || !byteArray.length) return "";
634 if (!(byteArray instanceof Uint8Array))
635 byteArray = new Uint8Array(byteArray);
636
637 try {
638 const str = new TextDecoder("utf-8", {fatal: true}).decode(byteArray);
639
640 if (str.length !== byteArray.length) {
641 if (isWorkerEnvironment()) {
642 self.setOption("attemptHighlight", false);
643 } else if (isWebEnvironment()) {
644 window.app.options.attemptHighlight = false;
645 }
646 }
647
648 return str;
649 } catch (err) {
650 // If it fails, treat it as ANSI
651 return Utils.byteArrayToChars(byteArray);
652 }
653 }
654
655
656 /**

Callers 13

arrayBufferToStrMethod · 0.80
fromBase64Function · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80
runMethod · 0.80

Calls 4

isWorkerEnvironmentFunction · 0.85
isWebEnvironmentFunction · 0.85
decodeMethod · 0.80
byteArrayToCharsMethod · 0.80

Tested by

no test coverage detected