GP32 My code to convert float to text on GP32


generalnmx

Playful/Fascist Mod
Joined
Apr 18, 2003
Messages
2,128
Age
43
Location
Maryland, USA
Website
www.matts-hosting.com
Since we can't use sprintf directly, here's the naive (really naive) way to do it:

Code:
dword FloatToChar(byte* text, float f, const dword percision)
{
    dword beDec, afDec;
    beDec = sprintf(text, "%d", (dword)f );
    text[beDec] = '.';
    afDec = sprintf(&text[beDec+1], "%d", (dword)((f * (float)percision) - ((dword)f * percision)) );
    return (beDec+afDec+1);
}

Where percision is 1 or 0, 10, 100, 1000, 10000, etc.

I'm sure someone has some better code, but this'll work until they post it.
 
maybe a stupid question, but please help!

are you using the "sprintf()" from <stdlib>? isn't it for x86?

how about gp_str_func.sprintf()? what are the difference?

Cheers




JRyouK
 
<_< devkitadv and especially ARM ADT both have libs compiled for ARM CPUs. That means whatever files you find in your include should have an ARM lib if you have the correct compiler downloaded.

The reason I provided this is because sprintf() for GP SDK is known to crash when trying to convert floats, although I must admit I haven't tried gp_str_func.sprintf(). However, since the SDK docs define it as...

Code:
int (*sprintf)(char * buf, const char * fmt, ...);

...I doubt it would make a difference.
 
sprintf doesnt work at all for my program...

float f = 2.14;
char c[60];
while(1)
{
sprintf(c,"%g",f);
GpTextOut(NULL, &gpDraw[nflip], 20, 1, (char*)c, 0);
GpSurfaceFlip(&gpDraw[nflip++]);
nflip &= 0x01;
}

Why?
 
Back
Top