hi fellas,
a couple days ago i started to tried to make a simple thing again.
the translation of an character to a binary-number.
After i succeeded i just wanted to make sure that i can change strings into a binary-number.
the little program below works really nice but the part to change it back is for me a bit unsure.
if you have any function or idea how to fix it would be appreciated.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_BUCH 255
int main () {
int temp1,temp2, x, y;
size_t len;
char Buchstabe[2]; /* Buchstabe is known as letter */
int binary[MAX_BUCH];
char *Wort ;
int *BinWort; /*BinWort is known as the array with the string wich you wanna convert into a binary-number*/
char puffer[MAX_BUCH]; /*input buffer*/
char *zgr;
int *binzgr;
zgr=Buchstabe;
binzgr= binary;
/* letters converted into a binary-number */
printf(" Bitte gebe einen Buchstaben ein : \n");
scanf("%c", Buchstabe);
temp1=(int)*zgr; /* changing the character into an integer value */
temp2=temp1;
printf("der Buchstabe wird binaer zu :\t" );
x=2*sizeof(int);
while(x--) /*output of the binary-number of one character */
{
binary[x] = (((temp2 >> x) & 1)+'0');
}
for(x=0; x <7; x++)
{
printf("%c",binary[x] );
}
printf("\n");
fflush(stdin); /*makin sure no letter or numeric value is buffered */
/* convertion of written strings into a binary-number */
printf(" Geben Sie ein Wort ein : \t");
fgets(puffer, MAX_BUCH, stdin);
Wort=(char *)malloc(strlen(puffer)+1); /*getting memory for the string*/
if(NULL == Wort) /*error handling */
{
printf(" nicht mehr genuegend Speicher vorhanden ! \n");
return 0;
}
strcpy(Wort,puffer);
len=strlen(Wort); /*displaying how much letters the input got */
printf(" laenge des Wortes : %d \n\t", len);
zgr=Wort;
BinWort=(int*)malloc(4*(len*sizeof(int))); /*getting enough memory for the convertion at the runtime */
if(NULL== BinWort )
{
printf("Fehler\n");
return 0;
}
/*changing the character into integer values */
for(y=0;y<(len-1); y++)
{
temp1=(int)Wort[y];
temp2=temp1;
x=((2*sizeof(len))-1); /* the last \0 not to display */
while(x--) /*convertion string -> binary and output */
{
BinWort[x] = (((temp2 >> x) & 1)+'0');
}
printf("\t\n");
for(x=0; x <(6*(len-1)); x++)
{
printf("%c",BinWort[x] );
}
}
printf("\n");
return 0;
}