PDA

View Full Version : Integer to String?


CroCo
01-22-2005, 04:56 AM
In the palm OS reference handbook I fond a function that converts Strings to Integer but cannot find one for the other way arround. Could someone enlighten me how to do it? I get messed up with the pointers.
If I do it like this (see function ButtonInput), I get memory readings from the bus when calling the function like this:

UpdateFiled(fldP, sting)

If I call UpdateField(fldP, "1"); it works just normal. So something is wrong with the pointers when I try to cast the Int into the string.

static void UpdateField (FieldType* fldP, char *newtext)
{

char *p;
FormType* frmP;
MemHandle h, oldHandle;

frmP = FrmGetActiveForm();
h = MemHandleNew(50);
p = (char *) MemHandleLock(h);
StrCopy(p, newtext); // you'd copy from saved data, presumably
MemHandleResize(h, StrLen(p)+1);
MemHandleUnlock(h);
//fldP = (FieldType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, MainFormFieldHH));
oldHandle = FldGetTextHandle(fldP);
FldSetTextHandle(fldP, h);
FldDrawField(fldP);
if (oldHandle)
MemHandleFree(oldHandle);
}

static Boolean ButtonInput (UInt16 *ActiveField, FormType* frmP, UInt8 num) //*ActiveField 0=HH, 1=MM, 2=SS; num entered number
{
Boolean handled = false;
FieldType* fldP;
char *string;

*string = num;
if (*ActiveField == 0)
{
fldP = (FieldType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, MainFormFieldHH));
UpdateField (fldP, string);
}
handled = true;
return handled;
}

Sharkk717
01-22-2005, 08:01 AM
Try StrIToA (look it up in the Palm OS pdf). Alternatively, you could just use StrPrintF with %u as the format param. Good luck, and if you need any more help you've found the right place!

regards, sharky

CroCo
01-22-2005, 08:15 AM
Hi, I found the function StrIToA(*str, Int) and it compieles well, but I guess some pointers are still messed up. I get the error message, that the prog caused a BUS error, from reading somewhere it shouldnīt.

Here is the way I did it:

Calling UpdateField like this works:
fldP = (FieldType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, MainFormFieldHH));

UpdateField(fldP, "0");

THIS does not work:

UInt8 num;
char *string;
StrIToA (string,num);
UpdateField(fldP, string); I get the BUS error.


static void UpdateField (FieldType* fldP, char* newtext)
{

char *p;
FormType* frmP;
MemHandle h, oldHandle;

frmP = FrmGetActiveForm();
h = MemHandleNew(50);
p = (char *) MemHandleLock(h);
StrCopy(p, newtext); // you'd copy from saved data, presumably
MemHandleResize(h, StrLen(p)+1);
MemHandleUnlock(h);
//fldP = (FieldType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, MainFormFieldHH));
oldHandle = FldGetTextHandle(fldP);
FldSetTextHandle(fldP, h);
FldDrawField(fldP);
if (oldHandle)
MemHandleFree(oldHandle);
}

static Boolean ButtonInput (UInt16 *ActiveField, FormType* frmP, UInt8 num) //*ActiveField 0=HH, 1=MM, 2=SS; num entered number
{
Boolean handled = false;
FieldType* fldP;
char *string;
StrIToA (string,num);
if (*ActiveField == 0)
{
fldP = (FieldType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, MainFormFieldHH));
UpdateField(fldP,string);
}
handled = true;
return handled;

Sharkk717
01-22-2005, 08:19 AM
Hi!

1) make sure you init your variables if they need to be initialized.
2) StrIToA returns a string :D it doesn't change the variable pointer.

UInt8 num;
char *string;
char *retStr;
retStr = StrIToA (string,num);

i just wrote that off the top of my head so it might need some tweaking, but give it a shot

regards, sharky

Tam Hanna
01-22-2005, 08:57 AM
BTW, if you want help directly from PalmSource themselves-join their developer forum mailing list!

CroCo
01-22-2005, 09:14 AM
To Tam Hanna:
Yep, signed up, just waiting for the confirmation mail:-)

To SharkBoy717:
Tried it already, but the same error pops up :-(

char *string;
string = "12"; // <-- like that it works also. It seems something is going wrong with what the StrIToA() does.
if (*ActiveField == 0)
{
fldP = (FieldType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, MainFormFieldHH));
UpdateField(fldP,string);
}

Sharkk717
01-22-2005, 09:27 AM
hi CroCo!

Just wrote this up in CW v9.2 (works perfectly for me):

char * strNum = "init";
char * strDisplay = "init";
int gerOne = 14;

strDisplay = StrIToA(strDisplay,gerOne);

FrmCustomAlert(MsgBox,strDisplay,"","");

regards, sharky

Sharkk717
01-22-2005, 09:29 AM
just looking through your code, you've never given the integer a value... are you trying to convert an Integer to a string, or a string to an integer? if you're trying to do an int to string you MUST give your int a value.

regards, sharky

CroCo
01-22-2005, 10:00 AM
Hi Sharkboy717
I went back in the palmOS programmers handbook and searched for the strIToA function and I found code, where it was used. I found out my mistake, the size of string was not good.

Like this:

char string [maxStrIToALen];
StrIToA (string,num);
if (*ActiveField == 0)
{
fldP = (FieldType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, MainFormFieldHH));
UpdateField(fldP, string);
}

everything workes fine.

BTW, the interger num gets a value, when I call the function ButtonInput(...)-
Alright, letīs see when I bump into new problems:-)
Thanks for the fast and enthusiastic help!! Hopefully this will be of help to other palmOS programming beginners:-))