[C++] Functions to Convert from string to wchar_t to char

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
lug
Posts: 79
Joined: Tue May 02, 2006 5:15 am
Contact:

[C++] Functions to Convert from string to wchar_t to char

Post by lug »

Here's a couple of functions that I had to research for the irrPreSettings project. Basically, the first one converts a string to wchar_t:

Code: Select all

    wchar_t stringToWchart(const std::string value)
    {
        // value to hold converted string
        c8 char_value[64];

        // convert from string to char
        strcpy_s(char_value,
                 std::strlen(value.c_str()) + 1,
                 value.c_str());

        // filled with number of characters converted
        size_t num_of_converted_wchars = 0;

        // value to hold converted wchar_t
        wchar_t wchar_value[64];

        // convert from char* to wchar_t*
        mbstowcs_s(&num_of_converted_wchars,
                   wchar_value,
                   std::strlen(char_value) + 1,
                   char_value,
                   _TRUNCATE);

        // return value
        return *wchar_value;
    }
The second one converts from wchar_t to char:

Code: Select all


    c8 wchartToChar(const wchar_t* value)
    {
        // convert from wchar_t* to char*
        size_t convertedChars = 0;

        // value to hold converted wchar
        c8 char_value[64];

        // convert from wchar_t* to char*
        wcstombs_s(&convertedChars,
                   char_value,
                   std::wcslen(value) + 1,
                   value,
                   _TRUNCATE);

        // return converted char
        return *char_value;
    }

I ended up not using either one though with the project updated to v1.2 of irrlicht. :oops: Anyway, I hope it's useful to somebody.
miko93
Posts: 54
Joined: Sat Nov 12, 2005 5:24 pm
Location: Regensburg, Germany
Contact:

Re: [C++] Functions to Convert from string to wchar_t to cha

Post by miko93 »

lug wrote:...
Anyway, I hope it's useful to somebody.
It is, Sir, it is. Thx.
"Yessir, I'll be happy to make these unnecessary changes to this irrelevant document." (Dilbert)
noreg
Posts: 158
Joined: Fri Jun 23, 2006 6:01 pm
Location: continental europe

Post by noreg »

Couldn't that solve my problem here ?
http://irrlicht.sourceforge.net/phpBB2/ ... scapemaker
Looks like the errors are in the conversion of the said functions.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, your problems are related to a change in the Irrlicht API which you have to adpat in the new scene nodes. There is no need for transformation methods.
noreg
Posts: 158
Joined: Fri Jun 23, 2006 6:01 pm
Location: continental europe

Post by noreg »

Thanks Hybrid. And if i didn't crosspost you even helped by mentioning the API-changes. :roll: How do i find out ?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Now, the problems are because you pass a wchar_t* to a function expecting char*. Irrlicht used wchar_t* to name scene nodes, but (I think since 1.0) uses char* because these names are internal anyway and so eveeryone can use plain chars. So you have to change these names all the way and convert all variables and constants used from using wchar_t to using char. This should be possible statically, i.e. directly in the program code, no need to make it during run-time.
The problematic methids are presented to you by the compiler, it's basically setName for all scene nodes.
maitrelame2
Posts: 26
Joined: Sun Jan 21, 2007 9:11 pm

Post by maitrelame2 »

That's great !

But can you tell me how to convert a char to wchar_t ?

THX
Irrlicht 4ever.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

This is what I do for both:

wchar_t to char:

stringc charconvert = (my wchar)

charconvert.c_str();

char to wchar_t:

stringw charconvert = (my char)

charconvert.c_str();
maitrelame2
Posts: 26
Joined: Sun Jan 21, 2007 9:11 pm

Post by maitrelame2 »

Excuse me but what is the mame of the output of the fonction for wchar_t to char ?

How do I get the name to send it with winsock ?

THX
Irrlicht 4ever.
Post Reply