Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 107
Registered: ‎02-15-2012
My Device: Blackberry Playbook 32GB
My Carrier: Virgin Mobile
Accepted Solution

Working with strings

Hi,

I've been working with c# for a long time now and have moved to obj-c to create an app for the playbook. Ive realised c# is much easier than objc when it comes to converting types.

 

Is there an acutal "String" type in obj-c? If not should I just use an array of chars?

Can someone explain to me how I would convert a float to a string/array of chars.

Can I use <string.h> in release (thining of the licence) since ive been using itoa to convert int to char array.

 

 

Any help would be greatly appreciated,

thanks

Mike

Developer
Posts: 120
Registered: ‎03-13-2012
My Device: Playbook, Dev Alpha C, Q10, Z10, Z30
My Carrier: T-Mobile

Re: Working with strings

Look up snprintf() and sprintf() in the C Library Reference.  That's how I do it.

Developer
Posts: 107
Registered: ‎02-15-2012
My Device: Blackberry Playbook 32GB
My Carrier: Virgin Mobile

Re: Working with strings

[ Edited ]

Are you sure they support floats? 

 

I have a float number. I need to print it to the console window to debug my app. I cant immediately put a float in to this char* so i need to convert the float to a string. How could i do this?

Developer
Posts: 120
Registered: ‎03-13-2012
My Device: Playbook, Dev Alpha C, Q10, Z10, Z30
My Carrier: T-Mobile

Re: Working with strings

If you're printing to the console, printf() works.  You just need to flush.  The printf() documentation is more thorough in how it handles formatting, sprintf() and snprintf() work the same way, just to a string.   How this helps.  If not I suggest a C language intro book that will go into more detail on printf().

Developer
Posts: 23
Registered: ‎05-29-2012
My Device: PlayBook
My Carrier: NA

Re: Working with strings

You can use printf to output floats. See http://www.cplusplus.com/reference/clibrary/cstdio/printf/ for a decent reference. You can convert a string to a float as well (see the atof() function).

 

Also... you mean "C", not "Obj-C" right? Obj-C is what iOS uses and "C","C++","Obj-C" are three different things. Can I ask why you're not using C++ for your project, or even Qt? I feel both are easier to use for application development than C.

Developer
Posts: 107
Registered: ‎02-15-2012
My Device: Blackberry Playbook 32GB
My Carrier: Virgin Mobile

Re: Working with strings

Yes sorry i mean C.

Well i started in C since i used the opengles 1.1 template. I just continued working on that cause ive never used opengl before. Ive only ever used directx. (from what ive been doing opengles 1.1 seems a heck of a lot more ”fixed“ than directx. But then maybe thats just mobile development). 

 

Are there any librarys i need to include to use atof()? 

Also what would the benefits be of using c++ instead of c? Would it overcomplicate it? Im a c# dev primarily so im used to having a lot of conversion calculations done for me lol.

Would you reccomend me to start again in c++? 

I wouldnt mind cause the main reason im doing this is to learn a new language.

 

Thanks

Highlighted
Developer
Posts: 23
Registered: ‎05-29-2012
My Device: PlayBook
My Carrier: NA

Re: Working with strings

You might need to include a specific header for atof -- google will tell you which one. I don't know anything about C#. C++ has the standard library that can make certain things easier for you, especially when it comes to data structures for managing lists and so on. It also allows you to use an OOP design pattern.

 

Whether or not you should restart in C++ depends on what you're actually trying to design; if you're just playing around/learning with tutorials then switching is probably not worth it. If you're designing a large application that would benefit from OOP, switch over to C++.

 

Also as a side note, imo Qt is much friendlier than straight C or C++ with the stdlib. For instance with your string problem, Qt's QString class has a super easy to use "toFloat()" method: http://qt-project.org/doc/qt-4.8/QString.html.