We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

split a float in characters

zulliger
zulliger asked
on
Medium Priority
223 Views
Last Modified: 2010-04-15
We are programming PIC16c73a with a C-Compiler (PCM). This is normal C without any special libraries.
Now we have a float (18.1235468), we have to output this number serial (I2C - Bus). We want only one number after the the point (18.1) to put this number serial, we need each numbe alone (n1 = 1; n2 = 8; n3 = 1......)
Can anybody help us???

Thank you very much!!
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
char n[100];
#include <stdio.h>
char n1,n2,n3,n4;
sprintf(n,"%.1f",18.1235468);
n1 = n[0];
n2 = n[1];
n3 = n[2];
n4 = n[3];
char buffer[100];
sprintf(buffer,"%.1f".float_number);

this will give you a buffer which you can send ofcourse use byte by byte :)

hope it helps
It seems ozo was earlier with the answer, so i suppose he should get the points. (sorry ozo, but i hadn't even seen your comment :)

Author

Commented:
sorry, can someone tell me, how to get the points to the right person??

.. Thank you for your answers!
just reject mine, and then ask ozo to answer it, then you can grade it
(also place a comment that ozo should be the only one to post the answer)

Author

Commented:
Ozo you can now post the answer...
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
ozo, perhaps you're right, but this way is better than getting someone in the end telling me i stole their points :)

Commented:
Ozo, your answer is wrong.  It will corrupt the stack (or other variables) with the terminating zero of the string.  It assumes a  maximum size of the fixed part.  It will not work on any architecture/compiler that choses to align the char variables on a word boudary, rearrange the order of the variables on the stack, put n2, n3 and/or n4 in registers, etc.

TheMadManiac's solution (except the typo - the second period should be a comma) is much safer.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
alexo, MadManiac and I both assumed a maximum size of 100,
which I admit is sloppy, but seems safe enough on the literal constant 18.1235468
On a variable you should check that the magnitude is < 1e96
I made no assumptions about whether n2, n3, n4 are in registers or stacked.

Commented:
Ozo, please disregard my comment.  I have no idea what posessed me to write what I did.  (I thought the first argument was n1 and not n.  I'm off to buy a bigger monitor, better glasses and a foot-from-mouth remover.)  Please accept my humble apologies.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.