Write a C program to convert ASCII to Binary.
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("\tCharacter ASCII Binary\n");
while (ch <= 'z') {
printf("\t%c\t%6d\t%d\n", ch,
toascii(ch), asciiToBinary(toascii(ch)));
ch++;
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Character ASCII Binary
a 97 1100001
b 98 1100010
c 99 1100011
d 100 1100100
e 101 1100101
f 102 1100110
g 103 1100111
h 104 1101000
i 105 1101001
j 106 1101010
k 107 1101011
l 108 1101100
m 109 1101101
n 110 1101110
o 111 1101111
p 112 1110000
q 113 1110001
r 114 1110010
s 115 1110011
t 116 1110100
u 117 1110101
v 118 1110110
w 119 1110111
x 120 1111000
y 121 1111001
z 122 1111010
Character ASCII Binary
a 97 1100001
b 98 1100010
c 99 1100011
d 100 1100100
e 101 1100101
f 102 1100110
g 103 1100111
h 104 1101000
i 105 1101001
j 106 1101010
k 107 1101011
l 108 1101100
m 109 1101101
n 110 1101110
o 111 1101111
p 112 1110000
q 113 1110001
r 114 1110010
s 115 1110011
t 116 1110100
u 117 1110101
v 118 1110110
w 119 1110111
x 120 1111000
y 121 1111001
z 122 1111010
This code gives error
ReplyDeleteUse header file. The function toascii() is a part of that library file.
Deletehi ascii value to bits converting
ReplyDelete#include
ReplyDelete#include
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 toascii(char ch){
int a=ch;
return a;
}
int main() {
int ch = 'a';
printf("\tCharacter ASCII Binary\n");
while (ch <= 'z') {
printf("\t%c\t%d\t%d\n", ch,toascii(ch), asciiToBinary(toascii(ch)));
ch++;
}
getch ();
return 0;
}