Created
August 10, 2023 22:30
-
-
Save StringManolo/8a9d83dfc809612c45e5d2e329102b6f to your computer and use it in GitHub Desktop.
5um basic
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
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