944,054 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 39817
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 26th, 2007
0

string to float

Expand Post »
how can i convert string os size[10] to float. also i cannot use atof or sscanf. im asked to use bitwise or anyother methos which will take less processiong time.
is there anyway. thank you
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shriagni is offline Offline
5 posts
since Apr 2007
Apr 26th, 2007
0

Re: string to float

there's a function in <cstdlib> called strtof (string to float) which can perfectly do that...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Apr 26th, 2007
0

Re: string to float

you could convert one character at a time. start out by multiplying the result by 10 add the character and subtract '0', similar to how you would convert an integer

  1. char inputstr[] = "123.45";
  2. float result = 0.0F;
  3.  
  4. // convert 1st character
  5. result = (result * 10) + inputstr[i] - '0';

That will work for all the characters that preceed the decimal point. After that you need another float, do similar math as above (but you don't need to multiply by 10), divide the result by (10 * number of decimal digits) and finally add that to the result. Example:

  1. char inputstr[] = "123.45";
  2. float result = 0.0F;
  3. float temp;
  4. int nDecimalDigits;'
  5. // convert 1st character
  6. nDecimalDigits = 1; // initialize this outside the loop
  7.  
  8.  
  9.  
  10. temp = inputstr[i] - '0';
  11. nDecimalDigits *= 10;
  12. temp /= nDecimalDigits;
  13. result += temp;
  14.  

What's not shown above is that you have to put that code in a for-next loop with i as the loop counter.

Confused now?
Last edited by Ancient Dragon; Apr 26th, 2007 at 10:52 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Apr 26th, 2007
0

Re: string to float

sorry sir, im bit confused rite now. i dun really follow ur code. im sorry.
actually my string is dis. out[]="0303.4578"
i need to convert to float i=0303.4578.
thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shriagni is offline Offline
5 posts
since Apr 2007
Apr 26th, 2007
0

Re: string to float

If you can't use build-in standard C functions, then you need to figure out what I posted.

Any decimal digit in a string can be converted to its binary equlivalent by simply subtracting the ascii value of '0'. For example, '1' - '0' = 1. If you look up the values of '1' and '0' in a standard ascii chart you will find that a '1' has a decimal value of 49, and a '0' has a decimal value of 48. So 49 - 48 = 1.

After converting the digit to binary as previously explained you have to add it to the previous value of the final result. First shift all the digits previously converted to the left by 1 place, which is done by simply multiplying the value by 10, when add the value of the new decimal digit.

Now loop through all the digits in the string you posted and do the math as I illustrated previously for each digit. Give it a try and post your code (in code tags), and ask lots of questions if you need to.
Last edited by Ancient Dragon; Apr 26th, 2007 at 11:12 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Apr 26th, 2007
0

Re: string to float

dear Ancient Dragon ,
i think this is code as u explained to me so i tried out the code not sure which to print out so printed out all the results.
  1. /********************************************/
  2. main()
  3. {
  4. char inputstr[]="123.45";
  5. float result= 0.0F;
  6. float temp;
  7. int ndecimaldigit,i;
  8.  
  9. ndecimaldigit = 1;
  10.  
  11. for(i=0;i<=4;i++)
  12. {
  13. temp=inputstr[i] - '0';
  14. ndecimaldigit *= 10;
  15. temp /= ndecimaldigit;
  16. result += temp;
  17. }
  18. printf("%f %f %d \n",result,temp,ndecimaldigit);
  19. }
  20. /*********************/
but the o\p i got is
0.122671 -0.000129 -31072

wht mistake did i perform?

thank you for ur time n suggestion.
Last edited by WaltP; Apr 27th, 2007 at 1:40 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shriagni is offline Offline
5 posts
since Apr 2007
Apr 26th, 2007
0

Re: string to float

Click to Expand / Collapse  Quote originally posted by shriagni ...
dear Ancient Dragon ,
You want to make Ancient Dragon happy?. Learn here how to properly tag the code you post.

Click to Expand / Collapse  Quote originally posted by shriagni ...
i think this is code as u explained to me so i tried out the code not sure which to print out so printed out all the results.

wht mistake did i perform?

thank you for ur time n suggestion.
Also read these rules about posting.
That would be a good way of saying thanks.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 27th, 2007
0

Re: string to float

You need two loops, not 1.
For all digits before the decimal, you need to multiply the total by 10 then add the value. Byt the way use a while loop.

When you get to the decimal you need to start a new loop to deal with the characters after.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Apr 27th, 2007
0

Re: string to float

why dn't you use a much simple (though longer) way to do this... this would be the "amateur way":

  1. char string[]="31.245¨;
  2. char num;
  3. double div=1;
  4. float number=0;
  5. num=string[0];
  6. int i=0,decimal=1;
  7. bool p=false;
  8. while (string[i]!='\0'){
  9. num=string[i];
  10. if ((num=='0')||(num=='1')||(num=='2')||(num=='3')||(num=='4')||(num=='5')||(num=='6')||(num=='7')||(num=='8')||(num=='9')){
  11. number=number+float(atoi(num)-(atoi(num)-1));
  12. number=number*10;
  13. }else{
  14. p=true;
  15. }if (p)
  16. decimal++;
  17. }i++;
  18. }for (int i=0;i<decimal;i++);
  19. div=div*10;
  20. }number=number/div;
  21.  
and there it is...

it's not the best way to do it, but it works... personally i would not recommend u 2 use it... but, if nothing else works... it's better than nothing... it could keep u out of an emergency...
Last edited by Nichito; Apr 27th, 2007 at 2:27 am.
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Apr 27th, 2007
0

Re: string to float

thanks for that code.
But only problem is that i cannot use 2 much of library function(atoi) as it will take lot of processiing time and cycles. so i hve to shorten the code for better performance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shriagni is offline Offline
5 posts
since Apr 2007

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: basic I/O operation
Next Thread in C Forum Timeline: Question about including C files





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


Follow us on Twitter


© 2011 DaniWeb® LLC