So I am writing a program that takes input char by char and converts each char into a binary value. It then converts the binary to random letters, each letters ascii code value is even or odd, even representing 0's and odds representing 1's. My program fully works as intended EXCEPT with binary numbers that should have a 0 as the 1st digit.
For example.
"H" = "IVLSXDX" which converts to 1001000, the decimal value of "H".
Now the problem is with characters that have a preceding 0, such as:
"#" should convert to 100011, but in the end converts to 1000110, which is throwing off the value and turns "#" into "F".
Here is my code:
#include <stdio.h>
#include <stdlib.h>
/*Returns an even valued ascii uppercase letter. Random */
char genEven() {
int value = (rand() % 13);
char evens[13] =
{'B','D','F','H', 'J','L','N','P','R','T','V','X','Z'};
return evens[value];
}
/** Returns an odd ascii uppercase letter. Random */
char genOdd() {
int value = (rand() % 13);
char odds[13] =
{'A', 'C', 'E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y'};
return odds[value];
}
/* Given a decimal, returns 7 bit binary representation. */
int dToB(int n) {
int remainder;
int binary = 0;
int i = 1;
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}
/*Reverses binary number for string conversion. */
int revBin(int n) {
int remainder;
int reversedNumber = 0;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return reversedNumber;
}
/* Takes binary and converts to randomized letter combos. */
void bToS(int n) {
int i = 7;
int r = n % 10;
char tmp;
//FIX SO THAT IF A BINARY HAS A 0 in the highest order spot, print an even
while(i > 0) {
if(r == 0) {
tmp = genEven();
printf("(%c-%d)", tmp, tmp);
} else {
tmp = genOdd();
printf("(%c-%d)",tmp,tmp);
}
n = n/10;
r = n%10;
i--;
}
}
/* Discards to EOL. */
void discardToEOL() {
char ch;
do {
scanf("%c", &ch);
} while(ch != '\n');
}
/* Encodes text.*/
int encode(void) {
char ch;
printf("? ");
discardToEOL(); /*Discards any newlines from previous. */
/*Get Input */
do {
scanf("%c", &ch);
if (ch >= 0 && ch <= 128 && ch != 10) {
printf("\nBinary rep of %c= %d\n", ch, dToB(ch));
bToS(revBin(dToB(ch))); //Decimal to bin-> binary to string
}
} while(ch != '\n');
printf("\n");
}
/** given a binary number, convert to decimal. */
int binToDec(int binary)
{
int decimal = 0, i = 1, remainder;
/* Iterate until number becomes zero */
while (binary != 0)
{
remainder = binary % 10;
decimal = decimal + remainder * i;
i = i * 2;
binary = binary / 10;
}
printf("%c", decimal);
}
/* Decodes text.*/
int decode(void) {
char ch;
printf("? ");
int i = 0;
int bin = 0;
discardToEOL(); /*Discards any newlines from previous. */
/*Get Input */
do {
scanf("%c", &ch);
if (ch >= 0 && ch <= 128 && ch != 10) {
if(i <= 7) {
if(ch % 2 == 0) {
//Add 0 to end
bin = bin * 10; //FIX
} else {
//Add 1 to end
bin = (bin*10) +1; //FIX
}
i++;
}
if(i == 7) {
//Send in for decimal conversion
binToDec(bin);
//Reset binary number
bin = 0;
i = 0;
}
}
} while(ch != '\n');
printf("\n");
}
/* Main */
int main(void) {
int c = 1;
printf("C/c : Encode\nD/d : Decode\nQ/q : Quit\n");
while(c) {
printf("> ");
char choice;
scanf(" %c", &choice);
switch(choice) {
case 'c':
encode();
break;
case 'C':
encode();
break;
case 'd':
decode();
break;
case 'D':
decode();
break;
case 'q':
c = 0;
break;
case 'Q':
c = 0;
break;
default:
printf("Invalid Input.\n");
break;
}
}
}