As far as normal ascii is concerned can I compare a wchar_t with a char type to see if they are equal?

I think, but do not know, that the most common ASCII chars have the same integer value in char and wchar_t. Is that true? Do I need to be careful?

The application I'm writing will also compare punctuation like , . : ; \t \n

Summarising, in UNICODE c++ when is this valid:

wchar_t wcA = ...
char chB = ...
if (wcA == chB) {

Recommended Answers

All 5 Replies

As long as you stay with ASCII I see no problem.

I think that in the conditional expression the 8 bits variable gets promoted to a 16 bits wchar_t.

I agree with caligulaminus, that with ASCII it will not give problem. Since ASCII need 1 byte for its representation. But if you talk about UNICODE content, then its not advisable to do so. Because UNICODE may use multiple bytes for character representation.

> Do I need to be careful?

If you decide to be careful, the code would be something like:

wchar_t wc = L'A' ;
    char c = 'A' ;
    const auto& ctype = std::use_facet< std::ctype<wchar_t> >( std::locale() ) ;
    if( ctype.widen(c) == wc ) { /* whatever */ }

Thanks all. As long as my char code is less than 128 I think I'll be fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.