-
Unicode To ANSI FORMAT
Hi Everyone,
i have been assigned a task. i have to convert a 100 unicode files into ascii text files.
i have found out a method of identifying whether a file is unicode or not.
This method is working for all the unicode files. However I am not certain how do I automate the task for conversion. I have noticed that notepad allows me to save the files as text. This is not the best way to go about it as it involves opening the file and saving it again.
Does anyone know how do you convert a file from unicode to text? What does notepad do ? Can I use the same API call without bothering to open a file?
How do I do this in C++?
Cheers!
Abhijit
-
I think WideCharToMultiByte() or MultiByteToWideChar() make those conversions.
-
Could you please do me a favor and post some code?
Cheers!
Abhijit
-
Sorry, I don't know how to use these functions, but everything about them is here:
http://msdn.microsoft.com/library/de...icode_17si.asp
-
I needed something to relax.
This function returns TRUE if and only if the file was converted from UNICODE to ANSI. If it already was ANSI or if any error occurred (memory or IO) it returns FALSE.
If the error occurred when writing the final result back to the file the original file is GONE! You might want to change the function so that it has a target file different from the source.
Code:
// Assumes no file is larger than 0x7fffffff bytes.
BOOL ConvertFileToAnsi(LPCTSTR szFileName, BOOL bForce)
{
HANDLE hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return FALSE;
DWORD dwSize;
dwSize = GetFileSize(hFile, NULL);
if(liSize.LowPart == INVALID_FILE_SIZE)
{
CloseHandle(hFile);
return FALSE;
}
WCHAR *pwcBuffer = NULL;
pwcBuffer = (WCHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize+sizeof(WCHAR));
if(pwcBuffer == NULL)
{
CloseHandle(hFile);
return FALSE;
}
DWORD dwRead;
BOOL bRet = ReadFile(hFile, pwcBuffer, dwSize, &dwRead, NULL);
if(!bRet)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pwcBuffer);
return NULL;
}
bRet = IsTextUnicode(pwcBuffer, liSize.LowPart, NULL);
if(bRet)
{
int iNeed = WideCharToMultiByte(CP_ACP, 0, pwcBuffer, -1,
NULL, 0, NULL, NULL);
char *pcConverted = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, iNeed+1);
if(pcConverted == NULL)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pwcBuffer);
return FALSE;
}
iNeed = WideCharToMultiByte(CP_ACP, 0, pwcBuffer, -1, pcConverted, iNeed,
NULL, NULL);
HeapFree(GetProcessHeap(), pwcBuffer);
if(iNeed == 0)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pcConverted);
return FALSE;
}
dwRead = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
if(dwRead == INVALID_SET_FILE_POINTER)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pcConverted);
return FALSE;
}
bRet = SetEndOfFile(hFile);
if(!bRet)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pcConverted);
return FALSE;
}
bRet = WriteFile(hFile, pcConverted, iNeed, &dwRead, NULL);
HeapFree(GetProcessHeap(), pcConverted);
}
CloseHandle(hFile);
return bRet;
}