Uint8Array
About
The Uint8Array
is a built-in data structure in MagiScript which allows you to store an array of 8-bit unsigned integers (values between 0 and 255). This data structure is particularly useful when you need to store a large number of small integers in a memory-efficient way.
It is a specialized array, with fix length and restricted values.
Creating a Uint8Array
You can create aย Uint8Array
ย by calling theย newUint8Array
ย function with a single argument: the size of the array. For example, the following code creates aย Uint8Array
ย with 10 elements:
let myArray = newUint8Array(10);
Accessing Array Items
You can access the elements of aย Uint8Array
ย using the bracket notation ([]
). The index of the first element isย 0
, and the index of the last element isย length - 1
.
For example, the following code sets the first element of theย myArray
ย toย 42
:
myArray[0] = 42;
And the following code retrieves the value of the third element:
let thirdElement = myArray[2];
Querying the Length
You can query the length of aย Uint8Array
ย using theย length
ย property. For example, the following code prints the length of theย myArray
:
console.log(myArray.length);
Example
The following code creates a Uint8Array
with 5 elements, sets their values to 0, 1, 2, 3, and 4, respectively, and then prints them:
let myArray = newUint8Array(5);
for (let i = 0; i < myArray.length; i++) {
myArray[i] = i;
}
for (let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
This will output the numbers from 0 to 5.
Conclusion
TheUint8Array
is a memory-efficient data structure that allows you to store an array of 8-bit unsigned integers in MagiScript. You can create aUint8Array
by calling thenewUint8Array
function with a single argument: the size of the array. You can access the elements of aUint8Array
using the bracket notation, and query its length using thelength
property.
General
๐ First Steps
MagiScript Editor
๐ Basics
๐ Keyboard Shortcuts
๐ Running Your First Program
๐ App Store
๐ Debugging Techniques
Examples
๐ Atom Time
๐๏ธ Time Practice
๐ Atom Drum
๐๏ธ Atom Stack
๐ Atom Square
๐ Atom Level
๐ Atom THMPR
๐ Poker Hands
๐ Keyboard Numeric
๐ Keyboard NOKIA
๐ Keyboard Cursor
๐ Keyboard Media
๐ Keyboard Custom
Input/Output
๐ Buttons
๐ Vibration Motor
๐ RGB LED
๐ Devices
๐ PeekSmith
๐ Quantum
๐ Teleport
๐ Spotted Dice
๐ SB Watch
๐ IARVEL Watch
๐ Fossil Watch
๐ Cosmos Printer
๐ PeriPage Printer
๐ ATC Remote
๐ Labko Scrabble
๐ Bluetooth Keyboard
๐ Bluetooth Mouse
๐ Timers
๐ Database
๐ Events
๐ System (exit, sleep, rand)
๐ Objects (card, time)
Language
๐ Summary
๐ Comments
๐ Operators
๐ Control Flow
๐ Functions
๐ Numbers
๐ Strings
๐ Arrays
๐ Objects
๐ Uint8Array