In: Computer Science
Write a C program that
I have written the program using C PROGRAMMING LANGUAGE.
TEXT TO BINARY CONVERSION :
INPUT :
OUTPUT :
CODE :
/*Included the required libraries*/
#include <stdio.h>
#include <string.h>
#define MAX 256 // constant defined
//main method
int main() {
int num;
FILE *fp1, *fp2;
char ch, src[MAX], tgt[MAX];
/* input file name from the user */
printf("Enter your input file name: ");
scanf("%s", src);
/* output filename from the user */
printf("Enter your output file name: ");
scanf("%s", tgt);
//opened the source file in read mode
fp1 = fopen(src, "r");
if (!fp1) {
printf("Unable to open the input file!!\n");
return 0;
}
//opened the target file in read mode
fp2 = fopen(tgt, "wb");
if (!fp2) {
printf("Unable to open the output file!!\n");
return 0;
}
/* read data from input file and write
the binary form of it in output file*/
while (!feof(fp1)) {
/* reading one byte of data */
fread(&ch, sizeof(char), 1, fp1);
/* converting the character to ascii integer value */
num = ch;
/* writing 4 byte of data to the output file */
fwrite(&num, sizeof(int), 1, fp2);
}
/* close all opened files */
fclose(fp1);
fclose(fp2);
return 0;
}
BINARY TO TEXT CONVERSION :
INPUT :
OUTPUT
:
.db 0x57, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00,
0x69, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
.db 0x43, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00
.db 0x6F, 0x00, 0x00, 0x00, 0x67, 0x00,
0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00
.db 0x6D, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00
.db 0x61, 0x00, 0x00, 0x00, 0x74, 0x00,
0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x61, 0x00,
0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00
.db 0x20, 0x00, 0x00, 0x00, 0x61, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x78, 0x00,
0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
.db 0x66, 0x00, 0x00, 0x00, 0x69, 0x00,
0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00
.db 0x28, 0x00, 0x00, 0x00, 0x61, 0x00,
0x00, 0x00, 0x6E, 0x00, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00
.db 0x20, 0x00, 0x00, 0x00, 0x66, 0x00,
0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x29, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
.db 0x61, 0x00, 0x00, 0x00, 0x6E, 0x00,
0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
.db 0x77, 0x00, 0x00, 0x00, 0x72, 0x00,
0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x73, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
.db 0x74, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00
.db 0x20, 0x00, 0x00, 0x00, 0x61, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00
.db 0x69, 0x00, 0x00, 0x00, 0x6E, 0x00,
0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00
.db 0x79, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
.db 0x6C, 0x00, 0x00, 0x00, 0x65, 0x00,
0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00
.db 0x52, 0x00, 0x00, 0x00, 0x65, 0x00,
0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00
.db 0x73, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
.db 0x6E, 0x00, 0x00, 0x00, 0x61, 0x00,
0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00
.db 0x20, 0x00, 0x00, 0x00, 0x66, 0x00,
0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00
.db 0x64, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00
.db 0x6E, 0x00, 0x00, 0x00, 0x76, 0x00,
0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00
.db 0x74, 0x00, 0x00, 0x00, 0x73, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00
.db 0x74, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00
.db 0x20, 0x00, 0x00, 0x00, 0x61, 0x00,
0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00
.db 0x65, 0x00, 0x00, 0x00, 0x78, 0x00,
0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00
.db 0x66, 0x00, 0x00, 0x00, 0x69, 0x00,
0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00
.db 0x2E, 0x00, 0x00, 0x00, 0x2E, 0x00,
0x00, 0x00, 0xFFFF,
CODE :
/*Included the required libraries*/
#include <stdio.h>
#include <string.h>
#define MAX 256 // constant defined
//main method
int main() {
int num;
FILE *fp1, *fp2;
char ch, src[MAX], tgt[MAX];
unsigned short val, word_count;
char buffer[20];
/* input file name from the user */
printf("Enter your input file name: ");
scanf("%s", src);
/* output filename from the user */
printf("Enter your output file name: ");
scanf("%s", tgt);
//opened the source file in read mode
fp1 = fopen(src, "rb");
if (!fp1) {
printf("Unable to open the input file!!\n");
return 0;
}
//opened the target file in read mode
fp2 = fopen(tgt, "wt");
if (!fp2) {
printf("Unable to open the output file!!\n");
return 0;
}
/* read data from input file and write
the binary form of it in output file*/
word_count = 0;
while (!feof(fp1)) {
if (word_count == 0) {
fwrite("\t.db\t",1,5,fp2);
}
val = fgetc(fp1);
if (word_count < 15) {
sprintf(buffer, "0x%02X, ", val);
word_count++;
}
else {
sprintf(buffer, "0x%02X\n", val);
word_count = 0;
}
fwrite(buffer, 1, strlen(buffer), fp2);
}
/* close all opened files */
fclose(fp1);
fclose(fp2);
return 0;
}
Thanks..