Convert Float/Integer to Char[] ?

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Convert Float/Integer to Char[] ?

Post by Nandika » Sat Feb 16, 2013 10:20 pm

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
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Convert Float/Integer to Char[] ?

Post by SemiconductorCat » Sat Feb 16, 2013 10:49 pm

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.

Code: Select all

char buffer[1024];
sprintf(buffer,"%f",float_variable);
Check sprintf doc here:
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Convert Float/Integer to Char[] ?

Post by Nandika » Sat Feb 16, 2013 11:18 pm

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?
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Convert Float/Integer to Char[] ?

Post by SemiconductorCat » Sun Feb 17, 2013 12:17 am

>>
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?
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Convert Float/Integer to Char[] ?

Post by Nandika » Sun Feb 17, 2013 8:38 am

OK SemiconductorCat .... Thank you :clap:
Descriptive answer for me.
I understood about sprintf,printf and format string.
evangilbort
Sergeant
Sergeant
Posts: 12
Joined: Fri Jun 21, 2013 12:19 pm

Re: Convert Float/Integer to Char[] ?

Post by evangilbort » Thu Aug 01, 2013 3:58 pm

I have one more question how can I check if the chracters entered by the user is numeber or a chracter.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Convert Float/Integer to Char[] ?

Post by SemiconductorCat » Tue Aug 06, 2013 8:29 pm

evangilbort wrote:I have one more question how can I check if the chracters entered by the user is numeber or a chracter.
You could use the scanf function here. But using scanf is not recommended by experienced C++ programmers at
SE(stackexchange.com ).On scanf manual page it said,
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 read the documentation, (RTFD Read the ____ documentation, little bit rude , but you need to read it).

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.
Post Reply

Return to “C/C++ Programming”