NO

Author Topic: char to bin and back  (Read 3678 times)

cd

  • Guest
char to bin and back
« on: March 06, 2010, 03:07:31 PM »
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.
 
Code: [Select]
#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;
}

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: char to bin and back
« Reply #1 on: March 08, 2010, 09:21:10 AM »
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.

1.) I am not really sure what your problem is. Do you want to change a string of '0' and '1' to an character?

2.) Your program works nice but in the output there are left the lower significat bits and right the more significat bits, which is not a common notification. Do you want this? ( A character 'B' = 0x42 = 100 0010, you get for 'B' = 0100 001)
best regards
 Alex ;)

cd

  • Guest
Re: char to bin and back
« Reply #2 on: March 08, 2010, 10:51:17 PM »
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.

1.) I am not really sure what your problem is. Do you want to change a string of '0' and '1' to an character?

2.) Your program works nice but in the output there are left the lower significat bits and right the more significat bits, which is not a common notification. Do you want this? ( A character 'B' = 0x42 = 100 0010, you get for 'B' = 0100 001)

to 1. : you've got it , i wanna change it back into a character .

to 2.: uhm no not really wanted.i didn't noticed that, because i tried every  letter of the word "Hallo" didn't noticed the problem but thanks for makin sure it's working wright. maybe, to change the  first and the last array wich gets changed , fix the problem.

i made it under vista. the first time i wrote that code was a listing from a book under xp and after all i just got the intension to try to change strings into binarycode instead of one letter. After i solved it, i wanted to change it back into the normal integer / ascii code number to be able to change it back into a letter.

i just know how to make it on a piece of paper but not really how to change it into a working code.

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: char to bin and back
« Reply #3 on: March 09, 2010, 08:21:31 AM »
For the converting you coded there is also a short way itoa(number, string, 2), which translates the number number into the string string by the base 2.

For the direction of translation you need I know no short way. So you have to code it by yourself. You have a string with '0' and '1'. Set a pointer to the lower significant bit of your binary-string. In a loop add to your destination char 1 if the pointer shows '1'. If it was not the last bit of your string move the pointer and shift the result left by one bit.

Does this help you or should I code it. ;)


PS.: for the function itoa() you need the compiler-option -Go (in the IDE the project-option: Define compatibility names)
best regards
 Alex ;)