Convert from wchar_t to int

Started by
4 comments, last by facemn 15 years, 3 months ago
In C, how do I convert from a wchar_t to an int? I'm not all that familiar with C which is part of the problem. Thanks!
Advertisement
wchar_t ch;int i = (int) ch;
Sorry, I should have been more clear. I want the wchar_t value, which is in this case "51" to literally be the number 51.
There are three ways of understanding what you say here.

The first is that you have a wchar_t with a numeric value of 51 (namely, 0x0033) and you want to convert it to an integer. This is what my program does.

The second is that you have a wchar_t which contains two ASCII bytes, the first representing the character '5' and the other representing the character '1' (that is, the value 0x3531 on an appropriately-endian platform). You shouldn't be representing things like that.

The third is that you have an array of wchar_t which contains a null-terminated string equivalent to the literal L"51". In that case, you'll probably use something like swscanf.
Assuming you mean a null terminated wchar_t array interpreted as a string, in which case, you could use wtoi() on compilers that support it (it's not a standard function), swscanf(), or convert the wchar_t array to a narrow character array with wcstombs() and then use atoi() on that.
Thanks everyone, this is what I was after:

swscanf(textValue, L"%d", &intValue);

This topic is closed to new replies.

Advertisement