Last active 1 month ago

Uint8ArrayEncodeTextWithIcons.js Raw
1function encodeTextWithIcons(text) {
2 // Split text by icon pattern {number}
3 const parts = text.split(/(\{\d+\})/g);
4
5 const byteArray = [];
6
7 for (const part of parts) {
8 if (part === '') continue;
9
10 // Check if it's an icon pattern
11 const iconMatch = part.match(/^\{(\d+)\}$/);
12
13 if (iconMatch) {
14 // It's an icon - add the icon number as a byte
15 const iconNumber = parseInt(iconMatch[1]);
16 byteArray.push(iconNumber);
17 } else {
18 // It's regular text - convert each character to byte
19 for (let i = 0; i < part.length; i++) {
20 byteArray.push(part.charCodeAt(i));
21 }
22 }
23 }
24
25 return new Uint8Array(byteArray);
26}
27
28// Example usage:
29const text = "Hello {133} World {255} Test";
30const encoded = encodeTextWithIcons(text);
31
32console.log("Original:", text);
33console.log("Byte Array:", Array.from(encoded));
34console.log("Uint8Array:", encoded);
35```
36
37**Output example:**
38```
39Original: Hello {133} World {255} Test
40Byte Array: [72, 101, 108, 108, 111, 32, 133, 32, 87, 111, 114, 108, 100, 32, 255, 32, 84, 101, 115, 116]