Unicode to ascii conversion

Unicode programming is supposed to be easier with windows. But there are certain weird instances when we need to write some weird code too. This sample presents a code written in such a situation.

Usually if anybody wants to do unicode programming in MFC this is what will have to be done.
1. Make all the declarations using TCHAR type.
2. Use all the functions related to TCHAR
3. do a #define unicode.

The MFC framework in this case will automatically take care of converting all the strings to Unicode if such steps are followed. If it was not “#define unicode”, then all the strings will be treated as ASCII.

There are some operating system level points to be noted too. If the operating system is below win95, the default strings are ASCII only. Even if you send UNICODE strings, it will be converted to ASCII, do the manipulations, reconvert and return the UNICODE value.
If the OS is equal to and above NT/2000, the case is reverse. Even if we write ASCII code, it gets converted to UNICODE strings and after doing all manipulations get reconverted to ASCII.

There might be some circumstances where we’ll have necessities to convert a UNICODE text file to ASCII file. This happens if the application is pre-written without handling unicode and there is only one place where we need a UNICODE file to be handled.
This code sample demonstrates conversion of a unicode file to ASCII type in such circumstances.

//Check if the file is UNICODE
int IsUnicodeFile(char* szFileName)
{
FILE *fpUnicode;
char l_szCharBuffer[80];

//Open the file
if((fpUnicode= fopen(szFileName,"r")) == NULL)
return 0; //Unable to open file

if(!feof(fpUnicode))
{
fread(l_szCharBuffer,80,1,fpUnicode);
fclose(fpUnicode);
if(IsTextUnicode(l_szCharBuffer,80,NULL))
{
return 2; //Text is Unicode
}
else
{
return 1; //Text is ASCII
}
}
return 0; // Some error happened
}

The above function opens the file using normal fopen method and checks the first byte if it is unicode or ASCII. It returns 2 if it is unicode. The return value can be modified with any other values and even using enumerated data. This used IsTextUnicode function to check for the suitability of the text.

//Convert the file to ASCII type
CString ConvertFile(char *szFileName)
{
CString strTempFileName ;
CString strInputFileName;
strInputFileName = szFileName;
char TempPathBuffer[255];
GetTempPath(255,TempPathBuffer);
FILE *fpASCII;
CStdioFileEx fpUnicode;

strTempFileName = TempPathBuffer;
strTempFileName += "TempUnicodecheck.txt";

if(IsUnicodeFile(szFileName) == 2)      {
//Open the UNICODE file
if(!fpUnicode.Open(szFileName,CFile::modeRead|CFile::typeBinary))
{
printf("Unable to open the unicode filen");
return strInputFileName ;
}

//Create the temporary file
if((fpASCII = fopen(strTempFileName.operator LPCTSTR(),"w+"))==NULL)
{
fpUnicode.Close();
printf("Unable to open the output filen");
return strInputFileName;
}

CString strData;
while(fpUnicode.ReadString(strData))
{
strData += "n";
fwrite(strData,sizeof(char),strData.GetLength(),fpASCII);
}
fflush(fpASCII);
fclose(fpASCII);
fpUnicode.Close();
return strTempFileName;
}
else
{
return strInputFileName;
}
}

This second function ConvertFile,
1. Takes the input of a file name
2. checks if it is a unicode file
3. generates an ASCII file at the WindowsTemp directory.

These functions can be used together in an ASCII application. Especially if the old code was written with ASCII files and if the input file is suddenly changed to UNICODE file by an external application.