WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

String Manipulation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Unicode to Ansi Conversion

 Keywords: ascii ansi unicode file convert 

#DefineFunction AnsiToUnicode(Ansifile, unicodefilename)
   ; Convert  ANSI file to Unicode file.
	buf = BinaryAlloc(Filesize(Ansifile)*2+100);Double size to fit unicode
	;Reads file into buffer 
	BinaryReadEx(buf,1,Ansifile,0,-1)
	srctype = 0 ;ANSI
	targtype = 3 ;Unicode
	codepage = 0 ;ANSI
	BinaryConvert(buf, srctype, targtype, codepage, 0)
	;Adds the first two bytes FFFE (unicode file header)
	buf2 = BinaryAlloc(2)
	BinaryPoke(buf2, 0, 255);0xff = 255
	BinaryPoke(buf2, 1, 254);0xfe = 254
	BinaryCopy(buf,0,buf2,0,2)
	BinaryWrite(buf, unicodefile)
	BinaryFree(buf)
	Return 1
#EndFunction

#DefineFunction UnicodeToAnsi(Unicodefile,Ansifilename)
	; Convert Unicode file to ANSI file.
	buf = BinaryAlloc(Filesize(unicodefile)+1)
	;Reads file into buffer and removes the first two bytes (unicode file header)
	BinaryReadEx(buf,0, unicodefile,2,-1)
	srctype = 3 ;Unicode
	targtype = 0 ;ANSI
	codepage = 0 ;ANSI
	BinaryConvert(buf, srctype, targtype, codepage, 0)
	BinaryWrite(buf, Ansifile)
	BinaryFree(buf)
	Return 1
#EndFunction 


Ansifile = "C:\Temp\Ansi.txt"
unicodefilename = "C:\Temp\Unicode.txt"
AnsiToUnicode(Ansifile,unicodefilename)

unicodefile = "C:\Temp\Unicode.txt"
Ansifilename = "C:\Temp\Ansi.txt"
UnicodeToAnsi(Unicodefile,Ansifilename)

Exit

Article ID:   W15338
File Created: 2002:09:05:13:51:22
Last Updated: 2002:09:05:13:51:22