utf16LEtest
encoding in utf16le
Created by: 0xAlessandro
Installed 1 times
Viewed: 0
Category: Array
Created on: Sunday, August 31, 2025 at 11:14:20 AM
Updated on: Sunday, August 31, 2025 at 11:14:20 AM
Tag arguments
[
{
"type": "string",
"help": "This provides help on the argument",
"defaultValue": "test"
},
{
"type": "number",
"help": "This provides help on the argument",
"defaultValue": "0xff"
},
{
"type": "quotelessString",
"help": "This provides help on the argument",
"defaultValue": "encode"
},
{
"type": "boolean",
"help": "This provides help on the argument",
"defaultValue": "false"
}
]
Code
class utf16LEtest {
encode(input) {
const buf = new ArrayBuffer(input.length * 2);
const view = new DataView(buf);
for (let i = 0; i < input.length; i++) {
view.setUint16(i * 2, input.charCodeAt(i), true);
}
return Array.from(new Uint8Array(buf), (b) => String.fromCharCode(b)).join(
"",
);
}
decode(input) {
const bytes = new Uint8Array(Array.from(input, (c) => c.charCodeAt(0)));
const view = new DataView(bytes.buffer);
let result = "";
for (let i = 0; i < bytes.length; i += 2) {
result += String.fromCharCode(view.getUint16(i, true));
}
return result;
}
}