Solved

split a float in characters

Posted on 1998-09-15
11
192 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!!
0
Comment
Question by:zulliger
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1252727
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];
0
 
LVL 1

Expert Comment

by:TheMadManiac
ID: 1252728
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
0
 
LVL 1

Expert Comment

by:TheMadManiac
ID: 1252729
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 :)
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:zulliger
ID: 1252730
sorry, can someone tell me, how to get the points to the right person??

.. Thank you for your answers!
0
 
LVL 1

Expert Comment

by:TheMadManiac
ID: 1252731
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)
0
 

Author Comment

by:zulliger
ID: 1252732
Ozo you can now post the answer...
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1252733
Ok.
(although 2 minutes doesn't seem like much difference to exclude one of two identical answers,
we were probably typing at the same time, runnning the program to check syntax,
(although I don't know how my first two lines got swapped pasting it here)
it would seem fairer to me if there were a wayto split the points in such cases)
0
 
LVL 1

Expert Comment

by:TheMadManiac
ID: 1252734
ozo, perhaps you're right, but this way is better than getting someone in the end telling me i stole their points :)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1252735
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252736
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.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1252737
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.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which checksum is this? 7 142
distributed computing in Python 4 122
Line meaning 9 85
Assigning default value to structure in C for mutithread application 17 66
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now