char to listbox

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
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

char to listbox

Post by Watcher »

Hi I haven´t found a satisfying answer for question how to include char to listbox and back line from listbox to char. Thanks for answers.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

This may work:

char* cStr = "something";
wchar_t wStr[255];
swprintf(wStr, 255, L"%s", cStr);
listBox->addItem(wStr);

The following will probably not work :oops:
char cStr[255];
sprintf(cStr, "%s", listBox->getListItem(0));

*Edited due to Vitek's comments*
Last edited by JP on Mon Nov 06, 2006 6:08 pm, edited 1 time in total.
Image Image Image
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

Post by Watcher »

I´m afraid it doesn´t work

Code: Select all

invalid conversion from `int' to `const wchar_t*' 
initializing argument 2 of `int swprintf(wchar_t*, const wchar_t*, ...)' 
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

Post by Watcher »

And when I tried this, it converts only the last word of the char. For exampe if char is abcd, in lisbox is shown only d.

swprintf(wStr,L"%c", chattext);
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Strange, compiles fine for me :? (dev-c++)
Image Image Image
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

Post by Watcher »

Could you send me please what header files do you have included and what is their sequence? I moved with irrlicht.h and suddenly your code works, but i still got only the last letter of my char.
swprintf(wStr,255,L"%c", chattext);
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I didn't have any special header files included, it's just a standard C function.
Image Image Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Code: Select all

wchar_t wStr[255]; 
swprintf(wStr, 255, L"%s", myChar); 
listBox->addItem(wStr); 

char cStr[255]; 
sprintf(cStr, "%s", listBox->getListItem(0));
Both of these are dangerous and incorrect.

In the first one, you are passing a character [I'm assuming it is at least a wide character] where the runtime system is expecting an array or pointer. When the system reads the myChar parameter, it gets the character value and then treats it like a pointer to a string. At the very least I would expect that to crash.

In the second one, you are converting a wide character string to narrow string. Usually this requires some sort of special format string to indicate that the source is a wide string. Microsoft uses "%S", but other systems may do it differently.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Watcher,

What is chattext? Is it a char, wchar_t, char* or wchar_t*? I ask because it is not really clear what you are doing. Some of your comments seem to indicate it is a string of some sort, but others indicate it is just a character.
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

Post by Watcher »

It is just character

Code: Select all

char chattext;
And you were right it crashed. I had to use %c but it wrote to listbox only the last letter of my character.

I have char chattext = 'hello' and it wrote only o.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

"hello" is not a char, it's a char*; an array of chars basically. That's probably why it's only showing the last character. Change it to char* and it may work.

Vitek: by myChar i meant a char*, though it's not named very well :lol: And the latter one, converting wchar_t* to char* was just a guess really off the top of my head! I've edited my original post to improve it somewhat :lol:
Image Image Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It is just character

And you were right it crashed. I had to use %c but it wrote to listbox only the last letter of my character.

I have char chattext = 'hello' and it wrote only o.
The problem appears to be your understanding of characters and character strings. If you write this:

Code: Select all

  char chattext = 'hello';
you should get a compile error, or at the very least a warning. The text 'hello' is composed of 5 characters and you're trying to stuff that into the space for just one character.

If chattext is more than one character, it is a character string, array of characters, or more simply a string. You would write this

Code: Select all

  char* chattext = "hello";
Now chattext would be all 5 characters 'h', 'e', 'l', 'l', and 'o'.

Now back to your original problem... I think you are trying to make a listbox display a text string, and retrieve that string back from the list box. There is a silent issue at work here, and that is the conversion from wide to narrow characters and character strings. You can usually avoid the issue entirely by always using wide characters/strings. If you are always using wide strings then the answer is trivial...

Code: Select all

// add an item with the text "hello"
wchar_t* chattext = L"hello";
listBox->addItem(chattext);

// get the item text back as a wide string
wchar_t* itemtext = listBox->getListItem(0);
You could also use the wide string class provided by Irrlicht.

Code: Select all

// add an item with the text "hello"
core::stringw chattext(L"hello");
listBox->addItem(chattext.c_str());

// get the item text back as a wide string
core::stringw itemtext = listBox->getListItem(0);
If you must, for some reason, convert the from narrow to wide strings, then there are two ways. Irrlicht provides a mechanism to do this via the string conversion constructor.

Code: Select all

core::stringc chattextA("hello");
core::stringw chattextW(chattextA.c_str());
I don't believe that the conversion that is done here is correct, but it will probably work fine provided you are using the lower 128 ASCII characters.

There are the functions wcstombs and mbstowcs for doing the conversion correctly using locale information.
Post Reply