Results 1 to 5 of 5

Thread: Unicode To ANSI FORMAT

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    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
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    I think WideCharToMultiByte() or MultiByteToWideChar() make those conversions.
    VS.NET 2003

    Need to email me?

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228
    Could you please do me a favor and post some code?

    Cheers!
    Abhijit
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    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
    VS.NET 2003

    Need to email me?

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width