-
Notifications
You must be signed in to change notification settings - Fork 0
/
mes_hex.c
64 lines (60 loc) · 1.54 KB
/
mes_hex.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "mes_hex.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "constants.h"
int file2hexfile(const char* input_name, const char* output_name, const char* out_open_mode)
{
FILE* in = fopen(input_name, "r");
if (NULL == in) {
fprintf(err_log, "Error open file %s to hex convert\n", input_name);
return ERR_OPEN_FILE;
}
FILE* out = fopen(output_name, out_open_mode);
if (NULL == out) {
fprintf(err_log, "Error open file %s to hex convert\n", output_name);
fclose(in);
return ERR_OPEN_FILE;
}
int ch;
while (EOF != (ch = fgetc(in))) {
fprintf(out, "%02X", ch);
}
fclose(in);
fclose(out);
return NO_ERROR;
}
int hexfile2file(const char* input_name, const char* output_name, int check_file)
{
FILE* in = fopen(input_name, "r");
if (NULL == in) {
fprintf(err_log, "Error open file %s from hex convert\n", input_name);
return ERR_OPEN_FILE;
}
FILE* out = fopen(output_name, "w");
if (NULL == out) {
fprintf(err_log, "Error open file %s from hex convert\n", "data_in_norm.txt");
fclose(in);
return ERR_OPEN_FILE;
}
int ch;
int counter = 0;
char one_hex[3] = {0,0,0};
int pos = 0;
while (EOF != (ch = fgetc(in))) {
if (0 != check_file && 0 == isxdigit(ch)) {
fprintf(err_log, "Non hex digit in position = %d.\n", pos );
}
++pos;
one_hex[counter] = ch;
++counter;
if (2 == counter) {
counter = 0;
char norm_ch = strtol(one_hex, NULL, 16);
fputc(norm_ch, out);
}
}
fclose(in);
fclose(out);
return NO_ERROR;
}