Skip to main content
GameDev.net gamedev.net
🔒 Locked

Char to Float

Started by Mat1515 Apr 25, 2004 at 11:25 AM 14 replies 600+ views
Original Post
Mat1515
Mat1515
How can i convert a char buffer to a float? char buff[5] float i = buff[5]; Trying to do that!
Ut o not me again:) lol
nonpop
nonpop
Uh... what do you actually want the float to contain after the conversion?
Mat1515
Mat1515
ok

I reading text thats in a Edit box control but it stores the text into a char buffer.

I need to use the textnumbers in the buffer in my game as floats. Thats my problem
Ut o not me again:) lol
nonpop
nonpop
Oh, of course... use atof():

float i = atof(buff);

Mat1515
Mat1515
hmm that didn't really work

It just set it to 0

[edited by - Mat1515 on April 25, 2004 12:49:33 PM]
Ut o not me again:) lol
nonpop
nonpop
How didn't it work? Compiler errors? Incorrect results?
Note that you must #include <cstdlib> (or stdlib.h if you're not using C++) to tell the compiler that a such function exists.


[edited by - nonpop on April 25, 2004 12:52:07 PM]
Mulligan
Mulligan
It doesnt convert a char to a float, it converts a string of chars to a float.

See below

Clickers

Also, post the number that cont convert.


---------------------------------------------------------

check
out
these
pictures
of
mine



[edited by - Mulligan on April 25, 2004 12:55:36 PM]
nonpop
nonpop
Pshaw! I''ll just make a new post.

This works OK for me (VC++ toolkit 2003 compiler):

#include <cstdlib>
#include <iostream>

int main ()
{
char buf[5];
strcpy (buf, "0.01");
float i = (float) atof (buf);
std::cout << i << std::endl; // prints 0.01

return (0);
}

PeterTarkus
PeterTarkus
Your question is kind of ambiguous..

are the chars ASCII chars or are they bytes read from a binary file?

If they are ASCII chars and in the range 0..9 do this

float f = (float)( (int)charbuf[0] - (int)''0'' )

If it is binary data (?) you have to typecast the buffer

float f = *(float*)(charbuffer+ptr);
ptr += 4;
etc..

Your question really didn''t make alot of sense..


PeterTarkus
PeterTarkus
OK, I see what you were asking..

yes atof(...) will convert a NULL terminated string to a floating point number.

Sorry, I thought you wanted to individual characters (?) ( your example was clearly pointing to that ).

Peace
Mat1515
Mat1515
dkljsakjfas

Im getting angry.

This is what im trying to do



case WM_COMMAND:
switch(LOWORD (wParam))
{
case IDOK:
char buffer[5];
GetDlgItemText(hwnd, IDC_EDIT1, buffer, 5);


float i= (float)atof(buffer);
g_Physics.GR.First = i;
EndDialog (hDlg, 0);
return TRUE;
break;
}
break;
}
Ut o not me again:) lol
Mat1515
Mat1515
It keeps freakin setting the float 2 zero when i type a number other than 0 into the edit box
Ut o not me again:) lol
BriTeg
BriTeg
Hokey dinah. It amazes me how many "conversion" threads there are, and it amazes me more that I'm the only one who ever posts the incredibly simple:

sscanf(mybuff, "%f", &myfloat);

[edited by - BriTeg on April 25, 2004 1:32:41 PM]
Brianmiserere nostri <font color=#BC
nonpop
nonpop
quote:
Original post by BriTeg
the incredibly simple:

sscanf(mybuff, "%f", &myfloat);


Simple and oh-so-ugly
atof() really should work. Have you checked that buff[] will contain the correct data after GetDlgItemText()?
Mat1515
Mat1515
What is that function doing I don''t want to create a string I want to take a string and turn it into a float
Ut o not me again:) lol
Mat1515
Mat1515
YOUR A GENIOUS nonpop!!!

THANKS IT WORKS NOWlol
Ut o not me again:) lol

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.