| | |
binary to decimal
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
I read two binary numbers which are separeted by a coma(1010,1000111) from a texfile into two differen linkedlist as below:
Then I put them into array (I will show just for one number)
Then I will convert these binary numbers to decimal:
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..
C Syntax (Toggle Plain Text)
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); } } } } }
C Syntax (Toggle Plain Text)
curr = head; while(curr){ binary1[x]=curr->val; curr = curr->next ; x++; }
C Syntax (Toggle Plain Text)
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));
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
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 3
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.
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.
![]() |
Similar Threads
- Number System:Conversion from binary to decimal (C)
- binary to decimal in c++ code (C++)
- How do I Take in Hexidecimal input out put binary, decimal, and octal (Java)
- i need code for binary to decimal (it should be written in 'C') (C)
Other Threads in the C Forum
- Previous Thread: balanced k-way sort-merge
- Next Thread: del nth last node in a singly link list using only 1 traversal
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





