//-------------------------------------------------------------------
//-- ANSI and WIDE string conversions C++ sample - Copyright © 2006, Dave Moats
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-- Permission is granted to anyone to use this sample code for any purpose,
//-- including commercial applications, subject to the following restrictions:
//--
//-- The origin of this code must not be misrepresented;
//--       you must not claim that you wrote the original code.
//--       If you use this code, an acknowledgment in the
//--       documentation is requested - shown below:
//--
//-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
//--
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-- To use this sample, you must include the following header
//-------------------------------------------------------------------
#include <windows.h>

//-------------------------------------------------------------------
//-- Function:       toAnsi
//-- Arguments:    wchar_t *inStr - the string to convert
//-- Returns:       The converted string or NULL
//-- Description: Takes the incoming string and tries to convert it
//--
//-- NOTE: it is the calling function's responsibility to free up the
//--          the returned string by calling the appropriate memory
//--          deallocation function, in this case use free ( )
//-------------------------------------------------------------------
char *toAnsi ( wchar_t *inStr )
{
   // If the incoming string is empty, then we have no work to do
   // just return NULL
   if ( !inStr )
   {
         return NULL;
   }
  
   // set up our return variable and initialize to NULL
   char *outStr = NULL;

   // see how much space we need for the conversion
   int size = WideCharToMultiByte ( CP_ACP, 0, inStr, -1, NULL, 0, NULL, NULL );
  
   // allocate the memory we need
   outStr = ( char *)malloc ( size );
   if ( outStr )
   {
         // if the allocation was successful, empty the out the newly allocated space
         // and then convert the string
         ZeroMemory ( outStr, size );
         WideCharToMultiByte ( CP_ACP, 0, inStr, -1, outStr, size, NULL, NULL );
   }
  
   // return the resulting value
   return outStr;
}

//-------------------------------------------------------------------
//-- Function:       toWide
//-- Arguments:    char *inStr - the string to convert
//-- Returns:       The converted string or NULL
//-- Description: Takes the incoming string and tries to convert it
//--
//-- NOTE: it is the calling function's responsibility to free up the
//--          the returned string by calling the appropriate memory
//--          deallocation function, in this case use free ( )
//-------------------------------------------------------------------
wchar_t *toWide ( char *inStr )
{
   // If the incoming string is empty, then we have no work to do
   // just return NULL
   if ( !inStr )
   {
         return NULL;
   }
  
   // set up our return variable and initialize to NULL
   wchar_t *outStr = NULL;

   // see how much space we need for the conversion
   int size = MultiByteToWideChar ( CP_ACP, 0, inStr, -1, NULL, 0 );
           
   // allocate the memory we need
   outStr = ( wchar_t *)malloc ( size );
   if ( outStr )
   {
         // if the allocation was successful, empty the out the newly allocated space
         // and then convert the string
         ZeroMemory ( outStr, size );
         MultiByteToWideChar ( CP_ACP, 0, inStr, -1, outStr, size, NULL, NULL );
   }
  
   // return the resulting value
   return outStr;
}


Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.