944,208 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 839
  • C RSS
Nov 30th, 2007
0

binary to decimal

Expand Post »
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
Reputation Points: 11
Solved Threads: 0
Light Poster
sivaslieko++ is offline Offline
49 posts
since Dec 2006
Nov 30th, 2007
0

Re: binary to decimal

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.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
danzona is offline Offline
19 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: balanced k-way sort-merge
Next Thread in C Forum Timeline: del nth last node in a singly link list using only 1 traversal





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC