/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 2025

Dato un file di interi 32bit little-endian, traformarlo in big-endian.
*/

#include <iostream>

using namespace std;

int main(void) {
	const char * inputFile = "interi_littleEndian.bin";
	const char * outputFile = "interi_bigEndian.bin";
	
	FILE * fileIn = fopen(inputFile, "rb");
	
	if (fileIn != NULL) {
		FILE * fileOut = fopen(outputFile, "wb");
		
		if (fileOut != NULL) {
			uint32_t numero;
			
			while (fread(&numero, sizeof(numero), 1, fileIn) == 1) {
				uint32_t converti =
					((numero & 0x000000FF) << 24) |
					((numero & 0x0000FF00) << 8) |
					((numero & 0x00FF0000) >> 8) |
					((numero & 0xFF000000) >> 24);
					
				fwrite(&converti, sizeof(converti), 1, fileOut);
			}
			
			fclose(fileOut);
		} else {
			perror("Error (destination)");
		}
		
		fclose(fileIn);
	} else {
		perror("Error (source)");
	}
	
	return 0;
}