Jump to content
shai

create text file with windows-1255 encode

Recommended Posts

hi

i try to create text file with _FileCreate function that encode of this file will be "windows-1255" but i can't.
 
the file always created in utf8 encode.
 
i also tryed to do it with FileOpen($sFilePath, BitOR(2, 8))
 
and still is created in utf8 encode.
 
thanks for all your solutions.
Edited by shai

Share this post


Link to post
Share on other sites
and still is created in utf8 encode.

What are you writing to the file?

How do you determine it's encoding eand what is the encoding exactly.. UTF8 with or without BOM?

Jos

Share this post


Link to post
Share on other sites

What are you writing to the file?

How do you determine it's encoding eand what is the encoding exactly.. UTF8 with or without BOM?

Jos

 

writing simple text to the file

when i open the txt file in notepad++ i see that file is utf8 WO BOM

Share this post


Link to post
Share on other sites

writing simple text to the file

when i open the txt file in notepad++ i see that file is utf8 WO BOM

So it is a regular file and Notead++ opens it as UTF8 without bom... the new SciTE will do the same to support multibyte chaacters by default.

What is the simply text you write?

Does it contain multi-byte characters?

Show some code you use to create the file.

Jos

Share this post


Link to post
Share on other sites

No it is not....  it is both ansi and utf8 without BOM as there are no special characters in there to indicate either one.

Jos

Share this post


Link to post
Share on other sites

how i can check what is the real encoding of output file?

Edited by shai

Share this post


Link to post
Share on other sites

a/ă

Local $sFile = "FileToCheckEncoding.txt"
Local $hfOpen = FileOpen($sFile, 2 + 8)
FileWrite($hfOpen, "Đào Van Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 32)
FileWrite($hfOpen, "UTF16 Little Endian: Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 64)
FileWrite($hfOpen, "UTF16 Big Endian: Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 128)
FileWrite($hfOpen, "UTF8 (with BOM): Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 256)
FileWrite($hfOpen, "UTF8 (without BOM): Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

; * -----:|
Func _GetFileEncoding($sFile)
    ; /*********************************************************************** * Trong
    ; * ---:| Function......: _GetFileEncoding($sFile)
    ; * ---:| Description...: Get File Encoding!
    ; ************************************************************************ *
    Local $sEndoding = FileGetEncoding($sFile), $sEncode
    $sEncode = $sEndoding
    If $sEndoding = 0 Then $sEndoding = "ANSI"
    If $sEndoding = 32 Then $sEndoding = "UTF16 Little Endian"
    If $sEndoding = 64 Then $sEndoding = "UTF16 Big Endian"
    If $sEndoding = 128 Then $sEndoding = "UTF8 (with BOM)"
    If $sEndoding = 256 Then $sEndoding = "UTF8 (without BOM)"
    If $sEndoding = -1 Then SetError(1, 0, -1)
    Return SetError(0, $sEncode, $sEndoding)
EndFunc   ;==>_GetFileEncoding
; * -----:|
Edit:

In Notepad++

new_docs_encoding.png

Edited by Trong

Share this post


Link to post
Share on other sites

ok now i see that if i writing simple text is works

but if i writing text from HTTP.ResponseText object the file swich to utf8.

how i can keep the file in ansi format?

Share this post


Link to post
Share on other sites
i create this function:
Func utf8towindows1255($hFileorg, $hFiledest)
    Local $hFileOpen = FileOpen($hFileorg, 0)
    Local $sFileRead = FileRead($hFileOpen)
    FileClose($hFileOpen)
    FileClose(FileOpen($hFiledest, 2 + 8))
    Local $hfOpen = FileOpen($hFiledest, 1 + 8)
    FileWrite($hfOpen, $sFileRead)
    FileClose($hfOpen)
EndFunc
and i run it on file that is encode in utf8.
 
if i run it in my full script the output file is in utf8
but if i run it on new script with only this function the output file is in ANSI.
 
why?
Edited by shai

Share this post


Link to post
Share on other sites

Your OS Version? XP/7/8/10/x32/x64 ?
Your OS Language? English/China?
Your AutoIt Version? 3.3.6.1/3.3.8.1/3.3.10.2/3.3.12.0/3.3.13.19/AutoitCN-3.3.9.21 ?

It worked fine.

MsgBox(0,"","File Encoding is: "&utf8towindows1255("UTFFile.txt", "ansi.txt"))


Func utf8towindows1255($hFileorg, $hFiledest)
    Local $hFileOpen = FileOpen($hFileorg, 0)
    Local $sFileRead = FileRead($hFileOpen)
    FileClose($hFileOpen)
    FileClose(FileOpen($hFiledest, 2 + 8))
    Local $hfOpen = FileOpen($hFiledest, 1 + 8)
    FileWrite($hfOpen, $sFileRead)
    FileClose($hfOpen)
    Return _GetFileEncoding($hFiledest)
EndFunc

; * -----:|
Func _GetFileEncoding($sFile)
    ; /*********************************************************************** * Trong
    ; * ---:| Function......: _GetFileEncoding($sFile)
    ; * ---:| Description...: Get File Encoding!
    ; ************************************************************************ *
    Local $sEndoding = FileGetEncoding($sFile), $sEncode
    $sEncode = $sEndoding
    If $sEndoding = 0 Then $sEndoding = "ANSI"
    If $sEndoding = 32 Then $sEndoding = "UTF16 Little Endian"
    If $sEndoding = 64 Then $sEndoding = "UTF16 Big Endian"
    If $sEndoding = 128 Then $sEndoding = "UTF8 (with BOM)"
    If $sEndoding = 256 Then $sEndoding = "UTF8 (without BOM)"
    If $sEndoding = -1 Then SetError(1, 0, -1)
    Return SetError(0, $sEncode, $sEndoding)
EndFunc   ;==>_GetFileEncoding

Share this post


Link to post
Share on other sites
On 3/1/2015 at 8:55 PM, VIP said:

a/ă

 

Local $sFile = "FileToCheckEncoding.txt"
Local $hfOpen = FileOpen($sFile, 2 + 8)
FileWrite($hfOpen, "Đào Van Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 32)
FileWrite($hfOpen, "UTF16 Little Endian: Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 64)
FileWrite($hfOpen, "UTF16 Big Endian: Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 128)
FileWrite($hfOpen, "UTF8 (with BOM): Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

$hfOpen = FileOpen($sFile, 2 + 8 + 256)
FileWrite($hfOpen, "UTF8 (without BOM): Đào Văn Trong")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

; * -----:|
Func _GetFileEncoding($sFile)
    ; /*********************************************************************** * Trong
    ; * ---:| Function......: _GetFileEncoding($sFile)
    ; * ---:| Description...: Get File Encoding!
    ; ************************************************************************ *
    Local $sEndoding = FileGetEncoding($sFile), $sEncode
    $sEncode = $sEndoding
    If $sEndoding = 0 Then $sEndoding = "ANSI"
    If $sEndoding = 32 Then $sEndoding = "UTF16 Little Endian"
    If $sEndoding = 64 Then $sEndoding = "UTF16 Big Endian"
    If $sEndoding = 128 Then $sEndoding = "UTF8 (with BOM)"
    If $sEndoding = 256 Then $sEndoding = "UTF8 (without BOM)"
    If $sEndoding = -1 Then SetError(1, 0, -1)
    Return SetError(0, $sEncode, $sEndoding)
EndFunc   ;==>_GetFileEncoding
; * -----:|

Edit:

In Notepad++

new_docs_encoding.png

it works fine but I wanna set ini file in "UTF8 (without BOM)" encoding

but after I use iniwrite it backs as "ANSI" !!!!!!!

$hfOpen = FileOpen($sFile, 2 + 8 + 256)
iniWrite($sfile, "General", "Title", "AutoIt")
ConsoleWrite("Encoding of file " & $sFile & " is: " & _GetFileEncoding($sFile) & " - " & @extended & @CRLF)

output:

Encoding of file FileToCheckEncoding.txt is: ANSI - 0

please help

Edited by AlienStar

Share this post


Link to post
Share on other sites

INI files by design are not Unicode compatible. There are workarounds to get them to be able to utilize Unicode characters, but it's non-standard to use them with Unicode characters. 

So what you need to do is encode your section, key and value strings in UTF-8 and store that as if it was ANSI. That can't make the standard function behave strangely UNLESS you share this ini file with application not aware that sections, keys and values actually are UTF-8.

Note that the ini file is itself an ANSI file (no UTF-8 BOM).

 

jchd:

Here's the secret sauce behind the hood:

Local $sText = "中國 한국어 ไทย"
Local $sUTF8 = BinaryToString(StringToBinary($sText, 4), 1)
IniWrite("my.ini", "test", "key", $sUTF8)
Local $sRaw = IniRead("my.ini", "test", "key", "I'm not there")
Local $sValue = BinaryToString(StringToBinary($sRaw, 1), 4)
MsgBox(0, "Round-trip check", $sValue)

Share this post


Link to post
Share on other sites

Hi! 

I know this topic is a bit old, but after I tried almost everything and nothing didn't work out for me I feel obligated to share with you the answer i found working perfect for me. 

The trick is to create a txt file with ANSI encoding and store it somewhere. Then whenever you want to create an ANSI encoding file just copy the txt file with the ANSI encoding where you want to have a txt ansi encoding file and then just write lines in it and there you go :)  You are all set! 

MsgBox("","",FileGetEncoding("D:\.....template ANSI file path.....\UTD62_2019_ansi.txt"))

FileCopy("D:\.....template ANSI file path.....\UTD62_2019_ansi.txt","D:\.....where you want ansi file path.....\UTD62_2019_304_ansi.txt")
sleep(1000)
MsgBox("","",FileGetEncoding("D:\.....where you want ansi file path.....\UTD62_2019_304_ansi.txt"))

I hope this will help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...