Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //#include "test.h"
- #define MAX_ROW 100
- #define MAX_COLUMN 44
- #define MAX_HEX 26
- #define ERROR_FSRC_COR -1
- #define ERROR_NUMBER_COLUMN -2
- #define ERROR_NUMBER_ROW -3
- #define ERROR_INPUT -4
- #define ERROR_FSRC -5
- #define ERROR_NUMBER -6
- int bin_to_hex(int num, char m[], char new_m[])
- {
- int i;
- int k = 0;
- char rc;
- char t1, t2, t3, t4;
- while (num % 4 != 0)
- {
- i = num;
- while (i >= 0)
- {
- m[i+1] = m[i];
- i--;
- }
- m[0] = '0';
- num++;
- }
- //printf("%d--", num / 4);
- while (k / 4 <= num / 4)
- {
- t1 = m[k];
- t2 = m[k+1];
- t3 = m[k+2];
- t4 = m[k+3];
- //printf("|%c %c %c %c|", t1, t2, t3, t4);
- if ((t1 == '0') && (t2 == '0') && (t3 == '0') && (t4 == '0'))
- {
- rc = '0';
- }
- else if ((t1 == '0') && (t2 == '0') && (t3 == '0') && (t4 == '1'))
- {
- rc = '1';
- }
- else if ((t1 == '0') && (t2 == '0') && (t3 == '1') && (t4 == '0'))
- {
- rc = '2';
- }
- else if ((t1 == '0') && (t2 == '0') && (t3 == '1') && (t4 == '1'))
- {
- rc = '3';
- }
- else if ((t1 == '0') && (t2 == '1') && (t3 == '0') && (t4 == '0'))
- {
- rc = '4';
- }
- else if ((t1 == '0') && (t2 == '1') && (t3 == '0') && (t4 == '1'))
- {
- rc = '5';
- }
- else if ((t1 == '0') && (t2 == '1') && (t3 == '1') && (t4 == '0'))
- {
- rc = '6';
- }
- else if ((t1 == '0') && (t2 == '1') && (t3 == '1') && (t4 == '1'))
- {
- rc = '7';
- }
- else if ((t1 == '1') && (t2 == '0') && (t3 == '0') && (t4 == '0'))
- {
- rc = '8';
- }
- else if ((t1 == '1') && (t2 == '0') && (t3 == '0') && (t4 == '1'))
- {
- rc = '9';
- }
- else if ((t1 == '1') && (t2 == '0') && (t3 == '1') && (t4 == '0'))
- {
- rc = 'A';
- }
- else if ((t1 == '1') && (t2 == '0') && (t3 == '1') && (t4 == '1'))
- {
- rc = 'B';
- }
- else if ((t1 == '1') && (t2 == '1') && (t3 == '0') && (t4 == '0'))
- {
- rc = 'C';
- }
- else if ((t1 == '1') && (t2 == '1') && (t3 == '0') && (t4 == '1'))
- {
- rc = 'D';
- }
- else if ((t1 == '1') && (t2 == '1') && (t3 == '1') && (t4 == '0'))
- {
- rc = 'E';
- }
- else if ((t1 == '1') && (t2 == '1') && (t3 == '1') && (t4 == '1'))
- {
- rc = 'F';
- }
- new_m[k / 4] = rc;
- k += 4;
- }
- return (num / 4);
- }
- int read_array(FILE * f, char* m, int num_m[])
- {
- int i = 0;
- int j;
- int rc = 0;
- while (1)
- {
- if (feof(f))
- {
- break;
- }
- if (i <= 100)
- {
- j = 0;
- do
- {
- rc += fscanf(f, "%c", &m[i][j]);
- if ((m[i][j] == '\n'))
- {
- break;
- }
- else if ((m[i][j] != '0') && (m[i][j] != '1'))
- {
- if (feof(f))
- {
- rc++;
- break;
- }
- return ERROR_FSRC_COR;
- }
- if (j >= 100)
- {
- return ERROR_NUMBER_COLUMN;
- }
- j++;
- }
- while(1);
- num_m[i] = j;
- i++;
- }
- else
- {
- return ERROR_NUMBER_ROW;
- }
- }
- if (rc == 0)
- {
- return ERROR_NUMBER;
- }
- else
- {
- return i;
- }
- }
- void print_array(int * num_m, char m[][MAX_COLUMN], int num)
- {
- for (int i = 0; i < num; i++)
- {
- for (int j = 0; j < num_m[i]; j++)
- {
- printf("%c", m[i][j]);
- }
- printf("\n");
- }
- }
- int main(int argc, char **argv)
- {
- FILE * fsrc;
- FILE * fdst;
- char m[MAX_ROW][MAX_COLUMN];
- char new_m[MAX_HEX];
- int num_m[MAX_ROW];
- int new_num_m;
- int n;
- if (argc != 3)
- {
- printf("exemple.exe <file_source_name>.txt "
- "<file_destination_name>.txt");
- return ERROR_INPUT;
- }
- else
- {
- fsrc = fopen(argv[1], "r");
- fdst = fopen(argv[2], "w");
- fclose(fdst);
- fdst = fopen(argv[2], "a");
- if (!fsrc)
- {
- fclose(fsrc);
- return ERROR_FSRC;
- }
- n = read_array(fsrc, *m, num_m);
- if (n == ERROR_NUMBER)
- {
- printf("The file is empty.");
- }
- else if (n == ERROR_FSRC_COR)
- {
- printf("The file contains invalid characters.");
- }
- else if (n == ERROR_NUMBER_COLUMN)
- {
- printf("The file contains too long a number.");
- }
- else if (n == ERROR_NUMBER_ROW)
- {
- printf("Too many numbers in the file.");
- }
- for (int i = 0; i < n; i++)
- {
- new_num_m = bin_to_hex(num_m[i], m[i], new_m);
- for (int j = 0; j < new_num_m; j++)
- {
- fprintf(fdst, "%c", new_m[j]);
- }
- fprintf(fdst, "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement