| | |
String to Double Conversion without using strtod() or atof()
![]() |
use a pointer and convert each digit to binary. Here's how:
Now, what I posted above is not a complete program -- I purposely left out important code that will make it work. You also have to have a couple loops and change 10 to a variable that is incremented by 10 on each loop iteration.
C Syntax (Toggle Plain Text)
char str[] = "123.456"; char* ptr = str; float n = (*ptr - '0'); // before the decimal point float n = (*ptr - '0') / 10; // after the decimal point
Now, what I posted above is not a complete program -- I purposely left out important code that will make it work. You also have to have a couple loops and change 10 to a variable that is incremented by 10 on each loop iteration.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
Hi,
I am programming a pos terminal by using C Programming language. I cannot use strtod() or atof (since it just calls strtod in it).
Any advices on converting string to double without using these functions?
I think it's an incorrect question. Why pos terminal control program can't use strtod? Is it possible to use sscanf? Or you MUST write C-string to double conversion code from the scratch? If so, it's the other story...
•
•
Join Date: Nov 2008
Posts: 7
Reputation:
Solved Threads: 0
Using strtod causes a segment error, odd but true. About sscanf... Shame on me
You are right. sscanf can do it.
You are right. sscanf can do it. > Using strtod causes a segment error,
If that's your reason for NOT using it, then you've got bugs in your code. Simply using another function does not make those bugs go away.
All that will happen is that sooner or later, you'll stumble across some other problem. Then, as now, you'll imagine the problem is somewhere else, and ask how to do x without using y.
A simple 5-line program which calls strtod() on your target would prove whether the library for your target was working or not. If the simple test works, and using strtod() in your code doesn't, then you really need to look at your code.
Valid reasons for not using strtod() on a PoS target would be
- too many problems with rounding for numeric values (but that's just floats all over)
- code footprint of the strtod() function is too large.
If that's your reason for NOT using it, then you've got bugs in your code. Simply using another function does not make those bugs go away.
All that will happen is that sooner or later, you'll stumble across some other problem. Then, as now, you'll imagine the problem is somewhere else, and ask how to do x without using y.
A simple 5-line program which calls strtod() on your target would prove whether the library for your target was working or not. If the simple test works, and using strtod() in your code doesn't, then you really need to look at your code.
Valid reasons for not using strtod() on a PoS target would be
- too many problems with rounding for numeric values (but that's just floats all over)
- code footprint of the strtod() function is too large.
I agree with Salem. I have wrote POS terminal control program (some years ago). As usually, no problems with memory and floating point arithmetics on POS processors. Moreover, if strtod is too expensive then sscanf is too expensive, printf is too expensive and so on. Impossible. It's not so time/space consuming code.
•
•
Join Date: Nov 2008
Posts: 7
Reputation:
Solved Threads: 0
Salem,
I already tried creating a small app which calls strtod. And it failed. Besides, my app works on simulator, I guess this problem is not about my code.
ArkM,
Possibly you've programmed a different brand by using different SDK.
I just defined a double variable, a char array. An called strtod. Works on simulator but not on the device.
Here is the code:
double d;
char s[] = "0.5";
d = strtod(s, NULL);
I already tried creating a small app which calls strtod. And it failed. Besides, my app works on simulator, I guess this problem is not about my code.
ArkM,
Possibly you've programmed a different brand by using different SDK.
I just defined a double variable, a char array. An called strtod. Works on simulator but not on the device.
Here is the code:
double d;
char s[] = "0.5";
d = strtod(s, NULL);
![]() |
Other Threads in the C Forum
- Previous Thread: Pointer arrays and structures
- Next Thread: need help with validation
| Thread Tools | Search this Thread |
* adobe api array asterisks binarysearch calculate changingto char character cm copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory feet fgets file floatingpointvalidation forloop frequency function givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux looping loopinsideloop. lowest meter microsoft mqqueue mysql number oddnumber odf open openwebfoundation owf pdf performance posix power probleminc process programming pyramidusingturboccodes radix read recv recvblocked repetition research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard string suggestions systemcall threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windows.h windowsapi






