Converting a float to a char*

I am attempting to pull data from a gps and print it to an lcd. Fortunately, there's lots of examples for using both gps' and lcd's, unfortunately, I am struggling with printing that information to the lcd.

I found an excellent (working) example that pulls data from the gps and prints it to the serial port using a printFloat function. I am attempting to take this function and convert it to instead return a char* that I can then send to the lcd. The problem I am having is that when I do the conversion I do not get numbers in my resulting string.

The best way to show my issue is to supply the code. Here is the code for the original function (which works):

And here is the attempt at converting the code (along with some debugging code) that I can't get to work:

and here is my test call (note, outputting to serial instead of screen right now):

As you can see with my last link to the calls to the functions, I basically call the working function (that just prints to the screen) and then the non-working function (that returns a string). The output to serial of each of these calls should be the same, but here is the output (including the debugging code you see in the second link):

Using printFloat:
Lat/Long(float): 10000.00000, 10000.00000

using char:
Lat/Long(float):
finishedChar after intPart: bbbbb
'
b
b,
finishedChar after intPart: bbbbb
'
bbb

My suspicions are with itoa() or strcat() not returning what I expect. Can anyone take a look at this and offer advice as to what may be going on?

Thanks much for the help!

Use sprintf()

Described here

char buf[100];
sprintf(buf, "%f", x);

The result is in buf.
Choose an appropriate size, I just stuck in 100.

WARNING This code has not be tried.

You could also use atof, if you don't need formatting control over how the conversion occurs. It does not impose near the overhead of sprintf.

I believe the OP wants to get a char * (or char[]) value starting with a double.

The code has

  float flat, flon;
  unsigned long age;
  char* lcdOutput;
 
  gps.f_get_position(&flat, &flon, &age);

which returns float values. I think the char* is for the lcd.

For example, the OP's code is char* floatToChar(double number, int digits)

atof takes as a parameter a char* (or char[]) and returns a double:double atof(const char *str);

So, assuming the aim is to convert a double to a string, atof isn't appropriate.

HTH
GB

hi im strugaling to get my arduino to type a key on the computer. i am new at using arduinos and i hear there are multaple steps involved but i dont have the slightest idea how to go about doing this. any code, info, tips would be helpfull. thanks.

gbulmer is right.

What I need to do is create a char* from several floats. That is, I get the floats for lat and long from the gps and then I need to create a string based on it. It seems like the code I supplied via pastebin should work, but you can see I'm getting completely nonsensical results from it.

Do you guys have any suggestions on how to fix my code?

Thanks.

Edit: Oops, I had missed gbulmer's suggestion above. Thanks, I'll give that a shot tomorrow and see if it works any better.

Out of curiosity, do you happen to know why my code was not working?

Also, do you have a way for me to get a char*? If I put in, say char x[100] then when I call lcd.print() I'll get an error since it requires a char*.

Your original code contains this stuff:

char* intPartStr;
char* finishedChar;

itoa(int_part, intPartStr, 10);
strcat(finishedChar, intPartStr);

intPartStr is a pointer to a character (or character array). What is it pointing to?
finishedChar is a pointer to a character (or character array). What is it pointing to?

This should work:

char intPartStr[20];
char finishedChar[50];

itoa(int_part, intPartStr, 10);
strcat(finishedChar, intPartStr);

Of course, you'd need to fix decimalValueStr, too.

Also, do you have a way for me to get a char*? If I put in, say char x[100] then when I call lcd.print() I'll get an error since it requires a char*.

I can't see a definition of lcd.print, but a simple way to get a char* from a char array:
char x[100];
lcd.print(&x[0]);
that is the address (a char *) of the first element in the array.

BUT, if I do:

void foo(char* p);
 
void setup() 
{ 
  // ...
} 
 
char x[100];

void loop() 
{ 
  foo(x);
}

void foo(char* p)
{
  Serial.print(p);
}

I don't get any errors, so I don't think that is the problem.
A char array name (without [...]) is so close to a char* that it shouldn't matter.

HTH
GB

jbbiz

hi im strugaling to get my arduino to type a key on the computer.

This thread is not about that subject. Asking the same question over many threads will not help. Start a thread, ask the question as well as you can, and try to concentrate on that thread and help folks understand what you need.

Heh, I was just going to ignore the attempted thread jack :slight_smile:

I'll give your suggestions a shot today and get back to you guys. Thanks.

PaulS: Ah, I see. It's been a while since I worked with a language where you can manipulate pointers directly, I'd misread what char* meant :slight_smile: I'll rework the code today using your and gbulmer's suggestions. Thanks!