char to listbox
char to listbox
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.
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
char cStr[255];
sprintf(cStr, "%s", listBox->getListItem(0));
*Edited due to Vitek's comments*
char* cStr = "something";
wchar_t wStr[255];
swprintf(wStr, 255, L"%s", cStr);
listBox->addItem(wStr);
The following will probably not work
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.
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*, ...)'
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));
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.
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.
Code: Select all
char chattext;
I have char chattext = 'hello' and it wrote only o.
"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 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
Vitek: by myChar i meant a char*, though it's not named very well 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
The problem appears to be your understanding of characters and character strings. If you write this: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.
Code: Select all
char chattext = 'hello';
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 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);
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);
Code: Select all
core::stringc chattextA("hello");
core::stringw chattextW(chattextA.c_str());
There are the functions wcstombs and mbstowcs for doing the conversion correctly using locale information.