10 examples of 'arraybuffer to string' in JavaScript

Every line of 'arraybuffer to string' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
7function arrayBufferToString(arrayBuffer) {
8 const byteArray = new Uint8Array(arrayBuffer);
9 const strArr = [];
10 for (let i = 0; i < byteArray.length; ++i) {
11 strArr[i] = String.fromCharCode(byteArray[i]);
12 }
13 return strArr.join('');
14}
182function arrayBufferToString(arrayBuffer) {
183 var byteArray = new Uint8Array(arrayBuffer);
184 var byteString = '';
185 for (var i = 0; i < byteArray.byteLength; i++) {
186 byteString += String.fromCharCode(byteArray[i]);
187 }
188 return byteString;
189}
3function ArrayBuffer2string (ar) {
4 var i, res, ar8;
5
6 ar8 = new Uint8Array(ar);
7 res = '';
8
9 for (i = 0; i < ar8.length; i++) {
10 res += String.fromCharCode(ar8[i]);
11 }
12
13 return res;
14}
25function base64ArrayBuffer(arrayBuffer) {
26 var base64 = ''
27 var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
28
29 var bytes = new Uint8Array(arrayBuffer)
30 var byteLength = bytes.byteLength
31 var byteRemainder = byteLength % 3
32 var mainLength = byteLength - byteRemainder
33
34 var a, b, c, d
35 var chunk
36
37 for (var i = 0; i < mainLength; i = i + 3) {
38 chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
39
40 a = (chunk & 16515072) >> 18
41 b = (chunk & 258048) >> 12
42 c = (chunk & 4032) >> 6
43 d = chunk & 63
44
45 base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
46 }
47
48 if (byteRemainder == 1) {
49 chunk = bytes[mainLength]
50
51 a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2
52 b = (chunk & 3) << 4 // 3 = 2^2 - 1
53 base64 += encodings[a] + encodings[b] + '=='
54 } else if (byteRemainder == 2) {
55 chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
56
57 a = (chunk & 64512) >> 10
58 b = (chunk & 1008) >> 4
59 c = (chunk & 15) << 2
60 base64 += encodings[a] + encodings[b] + encodings[c] + '='
61 }
62
63 return base64
64}
75function typedArrayToString(buffer) {
76 var string = '';
77 for (var i = 0; i < buffer.length; i+= 100)
78 string += String.fromCharCode.apply(undefined, buffer.subarray(i, i + 100));
79 return string;
80}
52static arrayBufferToString(arrayBuffer) {
53 if (typeof TextDecoder !== 'undefined') {
54 let textDecoder = new TextDecoder();
55 return textDecoder.decode(arrayBuffer);
56 } else {
57 let bytes = new Uint8Array(arrayBuffer);
58 let result = "";
59 let length = bytes.length;
60 for (let i = 0; i < length; i++) {
61 result += String.fromCharCode(bytes[i]);
62 }
63 return result;
64 }
65}
2function arrayBufferToBase64(buffer) {
3 return buffer.toString('binary');
4}
72export function buffer2string(buffer) {
73 const array = new window.Uint8Array(buffer);
74 const sliceSize = 8192;
75 const slices = [];
76 for (let i = 0; i < array.length; i += sliceSize) {
77 slices.push(String.fromCharCode.apply(null, array.subarray(i, i + sliceSize)));
78 }
79 return slices.join('');
80}
7function stringToArrayBuffer(str) {
8 var buf = new ArrayBuffer(str.length * 2);
9 var view = new Uint16Array(buf);
10 for (var i = 0, c = str.length; i < c; i++)
11 {
12 view[i] = str.charCodeAt(i);
13 }
14 return buf;
15}
67var arrayBufferToString = function arrayBufferToStringArray(buf) {
68 return String.fromCharCode.apply(String, arrayBufferToNumberArray(buf))
69 .replace('\0', '\u2400');
70};

Related snippets