0

I read two binary numbers which are separeted by a coma(1010,1000111) from a texfile into two differen linkedlist as below:

while (!feof(fout))
         {    
              ch=fgetc(fout);              
              if(ch==','){
                  printf("\n");
                  control=1;
              }else{
                   if(control==0){
                      if(ch=='1' || ch=='0'){
                        curr = (item *)malloc(sizeof(item));
                        curr->val = ch;
                        curr->next = head;
                        head = curr;
                        length1++;
                      }
                   }else{
                        if(!feof(fout)){
                           if(ch=='1' || ch=='0'){
                              curr2 = (item *)malloc(sizeof(item));
                              curr2->val = ch;
                              curr2->next = head2;
                              head2 = curr2;
                              length2=(length2+1);
                           }
                        }
                   }
              }
         }

Then I put them into array (I will show just for one number)

curr = head;
     while(curr){
          binary1[x]=curr->val;
          curr = curr->next ;
          x++;
     }

Then I will convert these binary numbers to decimal:

char number1[x];
     printf("\n\nBinary1:\t  ");
     for(i = 0; i < x; i++)
         number1[i]= (binary1[x-1-i]);                  
 
     for(i = 0; i < x; i++)
          printf("%c", number1[i]);
              
     printf("\nconversion is : %d\n", convert_bin2dec(number1));

But in my binary and number1 array that are some other special character that I could not recognize, and as a result of that convert_bin2dec function does not work
conver_bin2dec(char * str) accepts this:
char str[20]="1110";
as a paramater.

Why binary1 and number1 includes characters other than 1,0?
Please help..

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by danzona
0

I am making a bit of a guess here because you haven't posted all of your code, but I think you have declared something like:

char binary1[length1];

Then you copy the linked list into the char array called binary1.

Although you have asked for a specific amount of memory when you declared binary1, C doesn't do anything to insure that you stay within those bounds.

I think that your linked list includes a header. So when you walk the linked list you are walking down 5 nodes (header + 1 + 0 + 1 + 0).

After you have copied from the linked list to binary1, the value of x is 5. But you only have the first 4 values of binary1 set (1, 0, 1, 0). That 5th slot is random memory.

The first value that you copy into number1 is from binary[4] (the 5th slot), which could be anything, and is probably not a 0 or 1.

Hope this helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.