Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  111] [ 4]  / answers: 1 / hits: 23623  / 8 Years ago, tue, august 2, 2016, 12:00:00

I recently just started using webgland I am trying to understand the difference between Uint8Array, Uint16Array, Uin32Array. and how you would use them. I found some information about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array but can anyone tell me the difference between them and how you would use them?


More From » javascript

 Answers
14

Uint***Arrays construct non-typed arrays (commented by @zfor, so, for example, push would be undefined) with numbers only (still bytes). The difference is that each constructor array has different byte range in memory. Uint8Array has 1 byte only, then the limit of a number is 255. Uint16Array is 2 bytes long, then the limit is 65535. Uint32Array is 4 bytes long, so the limit is 4294967295.



When constructing a Uint*Array you declare the array length as the first argument:



var arr = new Uint8Array(1);


If you declare a array/buffer/object instead, the constructor still proccess them as a Uint*Array.



var arr = new Uint8Array([10, 257]);
console.log(arr[0]); // 10
console.log(arr[1]); // 1 (same thing: 257 % 256)


Now, see some examples:



arr[0] = 256;
console.log(arr[0]); // 0

arr[0] = 255;
console.log(arr[0]); // 255

[#61177] Friday, July 29, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devlin

Total Points: 474
Total Questions: 113
Total Answers: 100

Location: Sweden
Member since Fri, Apr 16, 2021
3 Years ago
devlin questions
Tue, Apr 27, 21, 00:00, 3 Years ago
Sat, Oct 31, 20, 00:00, 4 Years ago
Fri, Aug 28, 20, 00:00, 4 Years ago
;