VB icon

Ascii<-->Unicode conversion

Email
Submitted on: 1/3/2015 9:17:00 AM
By: Eugene Ciloci (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: C++ (general)
Views: 7937
 
     Two small and simple functions for converting from Ascii to Unicode and vice-versa
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Ascii<-->Unicode conversion
//**************************************
/*##############################################################################
CharsetConversion.h
Description: Ascii<->Unicode conversion
Author: Eugene Ciloci (ciloci@sympatico.ca) 
Last Modified: Sat Mar 30 02:07:54 2002
##############################################################################*/
#ifndef CHARSET_CONVERSION
#define CHARSET_CONVERSION
#include <stddef.h>
bool AsciiToUnicode(const char * szAscii, wchar_t * szUnicode);
bool UnicodeToAscii(const wchar_t * szUnicode, char * szAscii);
#endif
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Ascii<-->Unicode conversion
// Description:Two small and simple functions for converting from Ascii to Unicode and vice-versa
// By: Eugene Ciloci (from psc cd)
//
// Inputs:An Ascii or Unicode string
//
// Returns:On Success: true
On Error: false if any of the strings are NULL
//
// Assumes:Target string must be large enough to hold the converted string
//
// Side Effects:Modifies the target string
//**************************************

/*##############################################################################
CharsetConversion.cpp
Description: Ascii<->Unicode conversion functions
Author: Eugene Ciloci (ciloci@sympatico.ca)
Last Modified: Sat Mar 30 02:07:54 2002
##############################################################################*/
#include <string.h>
#include "CharsetConversion.h"
/*------------------------------------------------------------------------------
Purpose: Convert an Ascii string to Unicode
Parameters: [in] szAscii - Ascii string to be converted
		[out] szUnicode - Unicode string to receive conversion
On Success: szUnicode contains converted string.
		Returns true
On Error: false if either string is NULL
------------------------------------------------------------------------------*/
bool AsciiToUnicode(const char * szAscii, wchar_t * szUnicode)
{
	int len, i;
	if((szUnicode == NULL) || (szAscii == NULL))
		return false;
	len = strlen(szAscii);
	for(i=0;i<len+1;i++)
		*szUnicode++ = static_cast<wchar_t>(*szAscii++);
	return true;
}
/*------------------------------------------------------------------------------
Purpose: Convert a Unicode string to Ascii
Parameters: [in] szUnicode - Unicode string to be converted
			[out] szAscii - Ascii string to receive conversion
On Success: szAscii contains converted string
		Returns true
On Error: false if either string is NULL
------------------------------------------------------------------------------*/
bool UnicodeToAscii(const wchar_t * szUnicode, char * szAscii)
{
	int len, i;
	if((szUnicode == NULL) || (szAscii == NULL))
		return false;
	len = wcslen(szUnicode);
	for(i=0;i<len+1;i++)
		*szAscii++ = static_cast<char>(*szUnicode++);
	return true;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.