Skip to content

Instantly share code, notes, and snippets.

@StringManolo
Created August 10, 2023 22:30
Show Gist options
  • Save StringManolo/8a9d83dfc809612c45e5d2e329102b6f to your computer and use it in GitHub Desktop.
Save StringManolo/8a9d83dfc809612c45e5d2e329102b6f to your computer and use it in GitHub Desktop.
5um basic
import fs from 'fs/promises';
const sumBytesHexadecimal = async (inputFile: string) => {
try {
const outputFile = `${inputFile}.5um`;
const data = await fs.readFile(inputFile);
// Asegurarse de que la longitud sea múltiplo de 4
const paddedLength = Math.ceil(data.length / 4) * 4;
const paddedData = Buffer.alloc(paddedLength);
data.copy(paddedData);
const uint32Array = new Uint32Array(paddedData.buffer);
let sum = 0;
for (let i = 0; i < uint32Array.length; i++) {
sum += uint32Array[i];
}
const hexSum = sum.toString(16);
await fs.writeFile(outputFile, hexSum);
console.log(`Hexadecimal sum of bytes written to ${outputFile}: ${hexSum}`);
} catch (error) {
console.error('An error occurred:', error);
}
};
console.time('Execution Time');
sumBytesHexadecimal('text.txt').then(() => {
console.timeEnd('Execution Time');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment