Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Created September 28, 2021 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hashbrown777/fa3a77145ce74bae93e31573a8ee3ec3 to your computer and use it in GitHub Desktop.
Save Hashbrown777/fa3a77145ce74bae93e31573a8ee3ec3 to your computer and use it in GitHub Desktop.
Convert buffers of one word-length to another
function transferBytes(inputBuffer, inputLength, inputWordSize, wordSize, create) {
inputWordSize |= 0;
const inputBytes = inputLength * inputWordSize;
const inputBitsize = inputWordSize << 3;
wordSize |= 0;
const bytes = (inputBytes - 1 & -wordSize) + wordSize;
const bitsize = wordSize << 3;
const buffer = create(bytes, bytes / wordSize);
let inputIndex = -1;
let byteIndex = 0;
let word;
let alignment = 0;
let output = 0;
for (let index = 0; index < bytes; ++index) {
if (alignment <= 0) {
word = inputBuffer(++inputIndex);
alignment = inputBitsize;
}
alignment -= 8;
if ((index / wordSize | 0) > byteIndex) {
buffer(byteIndex, output);
++byteIndex;
output = 0;
}
else
output <<= 8;
output |= word >> alignment & 255;
}
if (output)
buffer(byteIndex, output);
}
function from8ToString(buffer) {
let output = '';
transferBytes(
(index) => (buffer[index]),
buffer.length,
1,
2,
(bytes, length) => {
return (index, word) => { output += String.fromCharCode(word) };
}
);
return output;
}
function from32To16(buffer) {
let output;
transferBytes(
(index) => (buffer[index]),
buffer.length,
4,
2,
(bytes, length) => {
output = new Uint16Array(length);
return (index, word) => { output[index] = word };
}
);
return output;
}
function fromStringTo32(string) {
let output;
transferBytes(
string.charCodeAt.bind(string),
string.length,
2,
4,
(bytes, length) => {
output = new Uint32Array(new ArrayBuffer(bytes));
return (index, word) => { output[index] = word };
}
);
return output;
}
function fromStringTo16(string) {
let output;
transferBytes(
string.charCodeAt.bind(string),
string.length,
2,
2,
(bytes, length) => {
output = new Uint16Array(new ArrayBuffer(bytes));
return (index, word) => { output[index] = word };
}
);
return output;
}
function fromStringTo8(string) {
let output;
transferBytes(
string.charCodeAt.bind(string),
string.length,
2,
1,
(bytes, length) => {
output = new Uint8Array(new ArrayBuffer(bytes));
return (index, word) => { output[index] = word };
}
);
return output;
}
@Hashbrown777
Copy link
Author

fromStringTo16('aaaa')
//Uint16Array(4) [97, 97, 97, 97]
fromStringTo16('aazaa')
//Uint16Array(5) [97, 97, 122, 97, 97]

fromStringTo8('aazaa')
//Uint8Array(10) [0, 97, 0, 97, 0, 122, 0, 97, 0, 97]
from8ToString(fromStringTo8('aazaa'))
//'aazaa'

fromStringTo32('azzaa')
//Uint32Array(3) [6357114, 7995489, 6356992]
fromStringTo32('aazzaa')
//Uint32Array(3) [6357089, 7995514, 6357089]
fromStringTo32('aazza')
//Uint32Array(3) [6357089, 7995514, 6356992]
fromStringTo32('aazz')
//Uint32Array(2) [6357089, 7995514]

fromStringTo32('aazza')
//Uint32Array(3) [6357089, 7995514, 6356992]
from32To16(fromStringTo32('aazza'))
//Uint16Array(6) [97, 97, 122, 122, 97, 0]
fromStringTo16('aazza')
//Uint16Array(5) [97, 97, 122, 122, 97]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment