Author Topic: Convert the char to an wchar_t  (Read 24089 times)

Schoppy

  • Guest
Convert the char to an wchar_t
« on: January 19, 2007, 09:18:26 am »
Hi,
I have a Problem. I must convert an Char to an wchar_t.

My Sytem: Code::Blocks 1 RC2 + MinGW + WindowsXP



void DLL_EXPORT * DEMOText(
                                char Text,
                                int iTopX, int iTopY, int iBotX, int iBotY,
                                bool boBorder)

{
// Convert the char(Text) to an wchar_t ???????


       return (void *)guienv->addStaticText(
            Text, //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this must a wchar_t
            rect<int>(iTopX, iTopY, iBotX+iTopX, iBotY+iTopY),
            boBorder);

}



Sorry for my bad English :-)
Can someone help me ?


Thanks
Schoppy





Offline TDragon

  • Lives here!
  • ****
  • Posts: 936
    • Twilight Dragon Media
Re: Convert the char to an wchar_t
« Reply #1 on: January 19, 2007, 04:40:36 pm »
I see you're using Irrlicht; a quick Google for "Irrlicht and wchar_t" turned up the mbstowcs function. Here's a quick question: do you really only want to pass a single character to your DEMOText function (that's what you're doing), rather than a string? Since I doubt you do, following is an improved version that should do what you want.

Code: [Select]
#include <cstdio>
#include <cstdlib>


IGUIStaticText* DLL_EXPORT DEMOText(char const* Text,
                                    int iX,
                                    int iY,
                                    int iWidth,
                                    int iHeight,
                                    bool boBorder)
{
    size_t alloc_len = strlen(Text) + 1;
    wchar_t* wc_out = new wchar_t[alloc_len];
    size_t result = mbstowcs(wc_out, Text, alloc_len);

    IGUIStaticText* ret_val = 0;
    if (result > 0)
    {
        ret_val = guienv->addStaticText(Text,
         rect< int >(iX, iY, iX + iWidth, iY + iHeight),
         boBorder);
    }

    delete[] wc_out;
    return ret_val;
}
« Last Edit: January 19, 2007, 04:50:53 pm by TDragon »
http://tdm-gcc.tdragon.net/ - TDM-GCC compiler suite for Windows (GCC 5.1.0 2015-06-28, 32/64-bit, no extra DLLs)

Offline kelo81

  • Multiple posting newcomer
  • *
  • Posts: 86
Re: Convert the char to an wchar_t
« Reply #2 on: January 19, 2007, 06:26:18 pm »
The irrclicht string implementation already has support for converting between both formats I think (char* and w_char*), I tried to assign some values between stringw and stringc and it works fine! :) The mbstowcs could be helpful for unique char operations.
Ezequiel Ruiz
Tango/04 consultant
www.tango04.com

Offline TDragon

  • Lives here!
  • ****
  • Posts: 936
    • Twilight Dragon Media
Re: Convert the char to an wchar_t
« Reply #3 on: January 19, 2007, 07:41:28 pm »
The irrclicht string implementation already has support for converting between both formats I think (char* and w_char*), I tried to assign some values between stringw and stringc and it works fine! :) The mbstowcs could be helpful for unique char operations.
Thanks for jumping in! I've never used Irrlicht on a regular basis, and wasn't aware of its string class.

Code: [Select]
IGUIStaticText* DLL_EXPORT DEMOText(char const* Text,
                                    int iX,
                                    int iY,
                                    int iWidth,
                                    int iHeight,
                                    bool boBorder)
{
    core::stringw wc_text(Text);
    return guienv->addStaticText(wc_text.c_str(),
     rect< int >(iX, iY, iX + iWidth, iY + iHeight),
     boBorder);
}
http://tdm-gcc.tdragon.net/ - TDM-GCC compiler suite for Windows (GCC 5.1.0 2015-06-28, 32/64-bit, no extra DLLs)

Schoppy

  • Guest
Re: Convert the char to an wchar_t
« Reply #4 on: January 19, 2007, 11:02:43 pm »
Thats it. Thank you for your help.  :D

Offline dgrafix

  • Single posting newcomer
  • *
  • Posts: 9
Re: Convert the char to an wchar_t
« Reply #5 on: April 14, 2008, 11:11:46 am »
Hi thanks for posting this, however, why is this needed?
delete[] wc_out;


Unless its returning the pointer shouldnt it automatically get dumped on function return?

Offline Deschamps

  • Multiple posting newcomer
  • *
  • Posts: 120
Re: Convert the char to an wchar_t
« Reply #6 on: April 14, 2008, 12:07:50 pm »
Quote from: dgrafix
however, why is this needed? delete[] wc_out;

AFAIK, all the dynamicly reserved memory *must* be explicitly unassigned using 'delete' for avoiding memory leaks, and obviously it must be done while the object is accesible (i.e. before returning the function).
Misquotations are the only quotations that are never misquoted