Created
September 28, 2021 13:27
-
-
Save Hashbrown777/fa3a77145ce74bae93e31573a8ee3ec3 to your computer and use it in GitHub Desktop.
Convert buffers of one word-length to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Author
Hashbrown777
commented
Sep 28, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment