Uint8Array to string in Javascript
TextEncoder
and TextDecoder
from the Encoding standard, which is polyfilled by the stringencoding library, converts between strings and ArrayBuffers:
var uint8array = new TextEncoder().encode("¢");
var string = new TextDecoder().decode(uint8array);
Converting from a Uint8Array to a string and back
You have code points in the array that you are trying to convert to utf-8
that don't make sense or are not allowed. Pretty much everything >= 128
requires special handling. Some of these are allowed but are leading bytes for multiple byte sequences and some like 254
are just not allowed. If you want to convert back and forth you will need to make sure you are creating valid utf-8
. The codepage layout here might be useful: https://en.wikipedia.org/wiki/UTF-8#Codepage_layout as might the description of illegal byte sequences: https://en.wikipedia.org/wiki/UTF-8#Invalid_byte_sequences.
As a concrete example, this:
let arr = new TextDecoder().decode(new Uint8Array([194, 169]))
let res = new TextEncoder().encode(arr) // => [194, 168]
works because [194, 169]
is valid utf-8 for © but:
let arr = new TextDecoder().decode(new Uint8Array([194, 27]))
let res = new TextEncoder().encode(arr) // => [239, 191, 189, 27]
doesn't because it's not a valid sequence.
Convert UInt8Array to String
This is not UTF-8 encoded text, but just binary data. So you can forget about the linked Q/A, you are not in the same situation.
Here it's all your choice as to how you'll encode it, some will prefer to convert it to an hex-dump
const arr = new Uint8Array([86, 201, 0, 216, 118, 231, 201, 251, 161, 22, 223, 14, 234, 229, 168, 146, 41, 121, 182, 136, 176, 120, 185, 173, 181, 47, 228, 244, 107, 230, 29, 27]);
const as_text = Array.from( arr )
.map( (val) => val.toString( 16 ).padStart( 2,"0" ) )
.join(" ");
console.log( as_text );
Converting between strings and ArrayBuffers
Update 2016 - five years on there are now new methods in the specs (see support below) to convert between strings and typed arrays using proper encoding.
TextEncoder
The TextEncoder
represents:
The
TextEncoder
interface represents an encoder for a specific method,
that is a specific character encoding, likeutf-8
,An encoder takes a stream of code points as input andiso-8859-2
,koi8
,
cp1261
,gbk
, ...
emits a stream of bytes.
Change note since the above was written: (ibid.)
Note: Firefox, Chrome and Opera used to have support for encoding
types other than utf-8 (such as utf-16, iso-8859-2, koi8, cp1261, and
gbk). As of Firefox 48 [...], Chrome 54 [...] and Opera 41, no
other encoding types are available other than utf-8, in order to match
the spec.*
*) Updated specs (W3) and here (whatwg).
After creating an instance of the TextEncoder
it will take a string and encode it using a given encoding parameter:
if (!("TextEncoder" in window)) alert("Sorry, this browser does not support TextEncoder...");
var enc = new TextEncoder(); // always utf-8console.log(enc.encode("This is a string converted to a Uint8Array"));
Convert Uint8Array to string in javascript
if you want the result as an array, you can split the string based on the newline character
and remove the last element
const resultArray = exeFiles.toString().split("\n")
resultArray.pop() // since last element will be empty string
console.log(resultArray);
Conversion between UTF-8 ArrayBuffer and String
function stringToUint(string) {
var string = btoa(unescape(encodeURIComponent(string))),
charList = string.split(''),
uintArray = [];
for (var i = 0; i < charList.length; i++) {
uintArray.push(charList[i].charCodeAt(0));
}
return new Uint8Array(uintArray);
}
function uintToString(uintArray) {
var encodedString = String.fromCharCode.apply(null, uintArray),
decodedString = decodeURIComponent(escape(atob(encodedString)));
return decodedString;
}
I have done, with some help from the internet, these little functions, they should solve your problems! Here is the working JSFiddle.
EDIT:
Since the source of the Uint8Array is external and you can't use atob
you just need to remove it(working fiddle):
function uintToString(uintArray) {
var encodedString = String.fromCharCode.apply(null, uintArray),
decodedString = decodeURIComponent(escape(encodedString));
return decodedString;
}
Warning: escape and unescape is removed from web standards. See this.
How to convert the object of Uint8Array to string?
While parsing, you need to send a reviver function, which conditionally parses the object. If you see how encoded looks, you will understand why the reviver function needs to be written this way.
How convert a string to type Uint8Array in nodejs
You can use Buffer.from(string[, encoding])
. The Buffer
class has implemented the Uint8Array
interface in Node since v4.x. You can also optionally specify an encoding with which to do string processing in both directions, i.e. buffer.toString([encoding])
.
How to convert uint8 Array to base64 Encoded String?
All solutions already proposed have severe problems. Some solutions fail to work on large arrays, some provide wrong output, some throw an error on btoa call if an intermediate string contains multibyte characters, some consume more memory than needed.
So I implemented a direct conversion function which just works regardless of the input. It converts about 5 million bytes per second on my machine.
https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
/*MIT LicenseCopyright (c) 2020 Egor NepomnyaschihPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.*/
/*// This constant can also be computed with the following algorithm:const base64abc = [], A = "A".charCodeAt(0), a = "a".charCodeAt(0), n = "0".charCodeAt(0);for (let i = 0; i < 26; ++i) { base64abc.push(String.fromCharCode(A + i));}for (let i = 0; i < 26; ++i) { base64abc.push(String.fromCharCode(a + i));}for (let i = 0; i < 10; ++i) { base64abc.push(String.fromCharCode(n + i));}base64abc.push("+");base64abc.push("/");*/const base64abc = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"];
/*// This constant can also be computed with the following algorithm:const l = 256, base64codes = new Uint8Array(l);for (let i = 0; i < l; ++i) { base64codes[i] = 255; // invalid character}base64abc.forEach((char, index) => { base64codes[char.charCodeAt(0)] = index;});base64codes["=".charCodeAt(0)] = 0; // ignored anyway, so we just need to prevent an error*/const base64codes = [ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 0, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51];
function getBase64Code(charCode) { if (charCode >= base64codes.length) { throw new Error("Unable to parse base64 string."); } const code = base64codes[charCode]; if (code === 255) { throw new Error("Unable to parse base64 string."); } return code;}
export function bytesToBase64(bytes) { let result = '', i, l = bytes.length; for (i = 2; i < l; i += 3) { result += base64abc[bytes[i - 2] >> 2]; result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)]; result += base64abc[((bytes[i - 1] & 0x0F) << 2) | (bytes[i] >> 6)]; result += base64abc[bytes[i] & 0x3F]; } if (i === l + 1) { // 1 octet yet to write result += base64abc[bytes[i - 2] >> 2]; result += base64abc[(bytes[i - 2] & 0x03) << 4]; result += "=="; } if (i === l) { // 2 octets yet to write result += base64abc[bytes[i - 2] >> 2]; result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)]; result += base64abc[(bytes[i - 1] & 0x0F) << 2]; result += "="; } return result;}
export function base64ToBytes(str) { if (str.length % 4 !== 0) { throw new Error("Unable to parse base64 string."); } const index = str.indexOf("="); if (index !== -1 && index < str.length - 2) { throw new Error("Unable to parse base64 string."); } let missingOctets = str.endsWith("==") ? 2 : str.endsWith("=") ? 1 : 0, n = str.length, result = new Uint8Array(3 * (n / 4)), buffer; for (let i = 0, j = 0; i < n; i += 4, j += 3) { buffer = getBase64Code(str.charCodeAt(i)) << 18 | getBase64Code(str.charCodeAt(i + 1)) << 12 | getBase64Code(str.charCodeAt(i + 2)) << 6 | getBase64Code(str.charCodeAt(i + 3)); result[j] = buffer >> 16; result[j + 1] = (buffer >> 8) & 0xFF; result[j + 2] = buffer & 0xFF; } return result.subarray(0, result.length - missingOctets);}
export function base64encode(str, encoder = new TextEncoder()) { return bytesToBase64(encoder.encode(str));}
export function base64decode(str, decoder = new TextDecoder()) { return decoder.decode(base64ToBytes(str));}
Related Topics
R Networkd3 Package: Node Coloring in Simplenetwork()
Xmlhttprequest Status 0 (Responsetext Is Empty)
How to Implement Jquery.Noconflict()
Es6 Template Literals VS. Concatenated Strings
How to Prevent Closing Browser Window
How to Make Cross-Domain Ajax Calls to Google Maps API
How to Format a Utc Date as a 'Yyyy-Mm-Dd Hh:Mm:Ss' String Using Nodejs
Shiny Slider Input Step by Month
Ckeditor 4: Uncaught Typeerror: Cannot Read Property 'Langentries' of Null
Get Gps Location from the Web Browser
Shiny App:Disable Downloadbutton
"Document.Getelementbyclass Is Not a Function"
Best Way to Display Flash Notices in Rails
How to Get Objects Value If Its Name Contains Dots
Why Is the Ecc-Dh Symmetric Key of This Site Different from Openssl