I tried to convert some Float value to Char array.But,I couldn't do it.
Try using internet.But,Hard to understand that code.
Anyone help me to solve this problem in simply.
Please convert 123.45 float value to char[] as an example.
Thanks
Convert Float/Integer to Char[] ?
- SemiconductorCat
- Major
- Posts: 455
- Joined: Mon Aug 22, 2011 8:42 pm
- Location: currently in hyperspace
Re: Convert Float/Integer to Char[] ?
it depends on the environment that you are in.
do you have access to standard C library?
Then use this function.
First make a buffer to store it. Then use `sprintf` function like this.
Check sprintf doc here:
do you have access to standard C library?
Then use this function.
First make a buffer to store it. Then use `sprintf` function like this.
Code: Select all
char buffer[1024];
sprintf(buffer,"%f",float_variable);
Re: Convert Float/Integer to Char[] ?
Thank you SemiconductorCat,
Is this sprint doc?
http://www.cplusplus.com/reference/cstdio/sprintf/
I must understand format string of C.
http://www.cplusplus.com/reference/cstdio/printf/
Please describe "%f" ?
What are you mean "environment" ?
Is it about C compiler ?
What is good Compiler for beginners?
Is this sprint doc?
http://www.cplusplus.com/reference/cstdio/sprintf/
I must understand format string of C.
http://www.cplusplus.com/reference/cstdio/printf/
Please describe "%f" ?
What are you mean "environment" ?
Is it about C compiler ?
What is good Compiler for beginners?
- SemiconductorCat
- Major
- Posts: 455
- Joined: Mon Aug 22, 2011 8:42 pm
- Location: currently in hyperspace
Re: Convert Float/Integer to Char[] ?
>>
What are you mean "environment" ?
Is it about C compiler ?
>>
Environment means , for a example PIC16FXXX environment does not provide floating point operations.
Here I mean compiler+library+hardware as the environment.It could refer to many things in computing world.
answer to the second question is yes and no. In C specification drafts, it's said that standard libraries are
a component of compiler itself. But in many compilers you could pass a switch and disable link with those
libraries. (example :- when we compile operating systems ). In GNU gcc we use ,`-nostdlib` option.
And please note that even these functions are part of standard library <stdlib.h> . Read this up too.
http://en.wikipedia.org/wiki/C_standard_library
Almost all hardware+OS+compilers support standard C library functions. But they may be limited as
stated in here.
http://www.mikroe.com/download/eng/docu ... ibrary.htm
MicroC have limited version of sprintf. And it may be very memory hungry because only around 60
byte SRAM is available for PICs. And standard "C" library is a platform independent library, but this may
not true in micro-controller world due to it's limitations as microc states. This will emit issues when
porting your PIC codebase to 8051 project. Or if you move microc to sdcc or some-other.
>> Please describe "%f" ?
You need to go through this. http://www.cplusplus.com/reference/cstdio/printf/
And review examples and it's output.Better if you cold compile and see.
that's %f in standard C. but in here, http://www.mikroe.com/download/eng/docu ... ibrary.htm it will refer to a insert a double.
And `sprintf` and `vprintf` are functions which you could input variable number of parameters.
If you can't understand that, refer : http://linuxprograms.wordpress.com/2008 ... or-printf/
. It's caled elipse operator. http://en.wikipedia.org/wiki/Ellipsis_% ... ramming%29
>> What is good Compiler for beginners?
Even there are limitations they are the limitations of the hardware of that microchip. It's not related to the
compiler. I don't see difference between compilers. But they have differences, that's why docs there
describe them. It's normal. You don't need to change your compiler for such a simple limitation. A compiler
that I worked with have no unicode support implemented on it's AROS OS. So I had to wrote all the Unicode
string copy, string length functions from scratch.This is a normal thing, don't bug your tools. Compilers are
your tools. A good crafts men does not blame his tools instead use them. Your compiler is oky. Even there were
limitations it provide that sprintf function for you right?
What are you mean "environment" ?
Is it about C compiler ?
>>
Environment means , for a example PIC16FXXX environment does not provide floating point operations.
Here I mean compiler+library+hardware as the environment.It could refer to many things in computing world.
answer to the second question is yes and no. In C specification drafts, it's said that standard libraries are
a component of compiler itself. But in many compilers you could pass a switch and disable link with those
libraries. (example :- when we compile operating systems ). In GNU gcc we use ,`-nostdlib` option.
And please note that even these functions are part of standard library <stdlib.h> . Read this up too.
http://en.wikipedia.org/wiki/C_standard_library
Almost all hardware+OS+compilers support standard C library functions. But they may be limited as
stated in here.
http://www.mikroe.com/download/eng/docu ... ibrary.htm
MicroC have limited version of sprintf. And it may be very memory hungry because only around 60
byte SRAM is available for PICs. And standard "C" library is a platform independent library, but this may
not true in micro-controller world due to it's limitations as microc states. This will emit issues when
porting your PIC codebase to 8051 project. Or if you move microc to sdcc or some-other.
>> Please describe "%f" ?
You need to go through this. http://www.cplusplus.com/reference/cstdio/printf/
And review examples and it's output.Better if you cold compile and see.
that's %f in standard C. but in here, http://www.mikroe.com/download/eng/docu ... ibrary.htm it will refer to a insert a double.
And `sprintf` and `vprintf` are functions which you could input variable number of parameters.
If you can't understand that, refer : http://linuxprograms.wordpress.com/2008 ... or-printf/
. It's caled elipse operator. http://en.wikipedia.org/wiki/Ellipsis_% ... ramming%29
>> What is good Compiler for beginners?
Even there are limitations they are the limitations of the hardware of that microchip. It's not related to the
compiler. I don't see difference between compilers. But they have differences, that's why docs there
describe them. It's normal. You don't need to change your compiler for such a simple limitation. A compiler
that I worked with have no unicode support implemented on it's AROS OS. So I had to wrote all the Unicode
string copy, string length functions from scratch.This is a normal thing, don't bug your tools. Compilers are
your tools. A good crafts men does not blame his tools instead use them. Your compiler is oky. Even there were
limitations it provide that sprintf function for you right?
Re: Convert Float/Integer to Char[] ?
OK SemiconductorCat .... Thank you
Descriptive answer for me.
I understood about sprintf,printf and format string.
Descriptive answer for me.
I understood about sprintf,printf and format string.
-
- Sergeant
- Posts: 12
- Joined: Fri Jun 21, 2013 12:19 pm
Re: Convert Float/Integer to Char[] ?
I have one more question how can I check if the chracters entered by the user is numeber or a chracter.
- SemiconductorCat
- Major
- Posts: 455
- Joined: Mon Aug 22, 2011 8:42 pm
- Location: currently in hyperspace
Re: Convert Float/Integer to Char[] ?
You could use the scanf function here. But using scanf is not recommended by experienced C++ programmers atevangilbort wrote:I have one more question how can I check if the chracters entered by the user is numeber or a chracter.
SE(stackexchange.com ).On scanf manual page it said,
so read the documentation, (RTFD Read the ____ documentation, little bit rude , but you need to read it).On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
so just review the following code.
Code: Select all
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
char buffer[1024];
int main(int argc,char **argv)
{
int i;
printf("Please enter a number:");
if( scanf("%d",&i) == 1)
{
printf("You entered :%d\n", i);
}else{
printf("You didn't enter a valid number.\n");
}
return 0;
}
The second method is , using DFD's (deterministic finite automata). If you have taken a general undergraduate
level compiler construction course then you should be aware of what is called DFD. Simply you could read the
input into a memory buffer and then go through DFD , if DFD last state is a end state of integer you could
say it's an integer. If error state then it's not a valid integer. I could guide you how to code a simple DFD,but
you need to cover the basics , first try to draw the DFD with pencil after you theoretically covered about DFD.
That's the theory behind how compilers lexical analyzer do work.