How to convert between Irrlicht-string to c string

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Preedak

How to convert between Irrlicht-string to c string

Post by Preedak »

I need to use the string that I get from a file or user input with the GUI components of the Irrlicht engine, but I cann't figure it out on how to do it yet.

This is the example of what I want to do.
{...
char name[40];
IGUIListBox* listbox;
...
FILE *fp;
fopen(fp,filename,"rt");
if(fp!=NULL)
{
fscanf(fp,"%s",name);
listbox->addItem(name);
....
}

I also need to get the string from the listbox back to my normal char[] string too.

It seems that the types do not match and I have to convert the variable, but I don't know how,so anyone can helpme out? Please. Thank you.
T101
Posts: 44
Joined: Thu Jul 29, 2004 4:41 pm

Post by T101 »

I've looked at the IGUIListBox header file, and it uses Windows-like wide char arrays.
These are 16 bit ints instead of 8-bit ints. They also probably use UCS-16 encoding.

Provided that only English characters are used, you can just copy using something like:

unsigned char mystring[]="Hello world";
unsigned wchar_t[BIGBUFFER] listboxentry; // I'm sure you know how to allocate on the fly if necessary

for(i=0;mystring;i++)
listboxentry=(unsigned wchar_t)mystring;

listboxentry=0;

And back in the same way.

If the encoding is different, it's not a lot of fun. You might need to use UTF-8 if it's for filenames.

Useful links:
http://www.joelonsoftware.com/articles/Unicode.html

http://www.unicode.org/
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

from API:

template<class T>
const T* irr::core::string< T >::c_str ( ) const [inline]

Returns character string

Returns:
Returns pointer to C-style zero terminated string.
Jedive
Posts: 146
Joined: Wed Apr 28, 2004 5:51 pm

Post by Jedive »

#include <stdlib.h>

wcstombs(); // Converts a Unicode string to an ASCII string
mbstowcs(); // Converts an ASCII string to an Unicode string
Guest

Post by Guest »

Thank you for everyone's help. I've used the solution suggested by T101 to get my text into a listbox already. And I've seem to be able to move on with my project. Thnak you very much.
Post Reply