Cannot convert char[] to wchar_t

MFC in Visual C++ 2010:
To print something in a Rich Edit Box, i use

m_rich.Insert(m_rich.GetLength(), L"Hello World");

If I want to print an integer which is variable, (i used to do this in VC++ 6.0)

1
2
3
4
int a=5;
char str[3];
itoa(a, str, 10);
m_rich.Insert(m_rich.GetLength(), str);


and it used to work.
In VC++ 2010, I get this error:-

'int ATL::CStringT<BaseType,StringTraits>::Insert(int,wchar_t)' : cannot convert parameter 2 from 'char [3]' to 'wchar_t'

Please help. Is there any alternative to the itoa conversion, which I have to do repeatedly.
Also tell me about the error and how can I get rid of it?
Last edited on
The Insert function is expecting a wide string - you are providing a normal ascii char string.

As you are on windows - look up the itow functionand see if that helps - (You may need to make the str array a bit bigger than 3 though.

EDIT:
Looking further , the error says:
int ATL::CStringT<BaseType,StringTraits>::Insert(int,wchar_t)
, so that function seems to be expecting just a single wchar_t value.
Last edited on
Thank you. I'am giving it a try.
It says there is no function as itow(). also length of str is okay here, coz its only gonna store a '3' and a NULL at the end. I tried this::

m_rich.Insert(m_rich.GetLength(), (wchar_t *)str);

It runs now but the output is ::
5쳌쳌쳌쳌쳌
One way to to convert a number to a string is to use CString, as it will be correct for the environment's Unicode setting.

1
2
3
4
5
6
7
8
9
// some number
int number = 5;

// convert to string
CString str;
str.Format("%d", number);

// use the string
m_rich.Insert(m_rich.GetLength(), str);
Well trying the CString method gave me the error:--

error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'

but I figured it out. guestgulkan was correct that itow should work, but actually the syntax would be _itow() not itow(). now it works. thanks.
You may have bigger problems. Why are some of your controls Unicode and others not?
Topic archived. No new replies allowed.