binary to decimal

Thread Solved

Join Date: Dec 2006
Posts: 49
Reputation: sivaslieko++ is an unknown quantity at this point 
Solved Threads: 0
sivaslieko++ sivaslieko++ is offline Offline
Light Poster

binary to decimal

 
0
  #1
Nov 30th, 2007
I read two binary numbers which are separeted by a coma(1010,1000111) from a texfile into two differen linkedlist as below:
  1. while (!feof(fout))
  2. {
  3. ch=fgetc(fout);
  4. if(ch==','){
  5. printf("\n");
  6. control=1;
  7. }else{
  8. if(control==0){
  9. if(ch=='1' || ch=='0'){
  10. curr = (item *)malloc(sizeof(item));
  11. curr->val = ch;
  12. curr->next = head;
  13. head = curr;
  14. length1++;
  15. }
  16. }else{
  17. if(!feof(fout)){
  18. if(ch=='1' || ch=='0'){
  19. curr2 = (item *)malloc(sizeof(item));
  20. curr2->val = ch;
  21. curr2->next = head2;
  22. head2 = curr2;
  23. length2=(length2+1);
  24. }
  25. }
  26. }
  27. }
  28. }
Then I put them into array (I will show just for one number)
  1. curr = head;
  2. while(curr){
  3. binary1[x]=curr->val;
  4. curr = curr->next ;
  5. x++;
  6. }
Then I will convert these binary numbers to decimal:
  1. char number1[x];
  2. printf("\n\nBinary1:\t ");
  3. for(i = 0; i < x; i++)
  4. number1[i]= (binary1[x-1-i]);
  5.  
  6. for(i = 0; i < x; i++)
  7. printf("%c", number1[i]);
  8.  
  9. 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..
Last edited by Narue; Nov 30th, 2007 at 2:52 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: danzona is an unknown quantity at this point 
Solved Threads: 3
danzona danzona is offline Offline
Newbie Poster

Re: binary to decimal

 
0
  #2
Nov 30th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC