Im trying to write a C program that will read an input text file by character, a
ID: 3668405 • Letter: I
Question
Im trying to write a C program that will read an input text file by character, and convert every character into Ascii equvalent, then convert ascii equvalent to binary.
Here's what i have so far:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fpointer;
fpointer = fopen("input.txt", "r");
char singlechar;
while ((singlechar = fgetc(fpointer)) != EOF){
printf("%c ", singlechar);
}
int iAsciiValue = (int)singlechar;
printf("%3d ", iAsciiValue);
fclose(fpointer);
system("pause");
return 0;
}
Explanation / Answer
Pleaase consider this answer by making your answer as void.
#include<stdio.h>
int main(){
char str[100];
int i=0;
printf("Enter any string: ");
scanf("%s",str);
printf("ASCII values of each characters of given string: ");
while(str[i])
printf("%d ",str[i++]);
return 0;
}
and for conversting the code to asci to binary then refer this code
#include <stdio.h>
int asciiToBinary(int input) {
int result = 0, i = 1, remainder;
/* convert decimal to binary format */
while (input > 0) {
remainder = input % 2;
result = result + (i * remainder);
input = input / 2;
i = i * 10;
}
/* print the resultant binary value */
return(result);
}
int main() {
int ch = 'a';
printf(" Character ASCII Binary ");
while (ch <= 'z') {
printf(" %c %6d %d ", ch,
toascii(ch), asciiToBinary(toascii(ch)));
ch++;
}
return 0;
}