The conversion from char string to a wchar array: is it needed.

Posted on 2005-04-18
Last Modified: 2012-05-05
I wanted to do a strncmp() between 2 strings
The problem is one argument is unsigned char *string  and the other is a
wide char array.

I wanted to know what is a good solution in this case.
Do I need to use mbstowcs/mbsnrtowcs  to convert the char string to a wide char array and then use oone of wchar compare APIs

.Just casting the arguments won't do I presume since storage requirements
are different.

Also are there any portability issues/limitations in doing this.

Question by:sanjay_thakur
    LVL 45

    Expert Comment

    Hi sanjay_thakur,

    You'll have to convert one of the string to be the same type as the other.  Comparing apples to apples, as it were.

    The other choice is to find/write an API that compares strings of dissimilar type.  Not really a recommended solution.

    Good Luck!
    LVL 4

    Author Comment

    Ok, I think the question boils down to
    How do I convert an unsigned char array to wchar_t array in an
    efficient manner using one of portable APIs.

    LVL 23

    Accepted Solution

    You will need to convert the normal string to a wide string using mbrtowc(),
    then compare the two wide strings using wcsncmp() as follows.

    #include <wchar.h>

    /* like strncmp() but compares a char string to a wchar string */
    int mixedcmp(const char * str1, const wchar_t * wstr2, size_t n)
          // alloc a temp buffer to hold the wide version of str1
          wchar_t wstr1 = alloca((n+1)*sizeof(wchar_t));

          // get a wchar representation of str
          mbrtowc(wstr1, str1, n, NULL);

          // do the comparison and return the result
          return wcsncmp(wstr1, wstr2, n);

    LVL 3

    Expert Comment

    There is no standard portable API's for converting from 8 bit ascii to unicode, but I am sure you will find some commercial solutions if you search for such.

    But, if you know that the values in your 8 bit ascii are just ordinary US characters you can create a simple conversion yourself:

    size_t i;
    for( i = 0; i < char_array_length; ++i )
       wchar_array[i] = (wchar)char_array[i];

    If there are requirements for handling other character sets and character values you just have to check the conversion tables at

    Unless you realy truly need a portable solution I would go for the solution from  brettmjohnson


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now