forked from TinyCC/tinycc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2hex.c
36 lines (29 loc) · 915 Bytes
/
bin2hex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: binary_to_hex input.bin output.hex\n");
return 1;
}
const char *inputFileName = argv[1];
const char *outputFileName = argv[2];
FILE *inputFile = fopen(inputFileName, "rb");
if (!inputFile) {
fprintf(stderr, "Error opening input file");
return 1;
}
FILE *outputFile = fopen(outputFileName, "w");
if (!outputFile) {
fprintf(stderr, "Error opening output file");
fclose(inputFile);
return 1;
}
unsigned char buffer[4];
while (fread(buffer, sizeof(unsigned char), 4, inputFile) == 4) {
unsigned int value = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
fprintf(outputFile, "0x%08X\n", value);
}
fclose(inputFile);
fclose(outputFile);
return 0;
}