Jump to content

Weird value for Chr function


Recommended Posts

Hi, not sure if there is a better way to do this because Chr() gives me issue.

$writeFile = FileOpen("ztest", 2)   ;open and erase
If Not ($writeFile = -1) Then
  MsgBox(0, "", Chr(Dec("8E")))
  FileWrite($writeFile, Chr(Dec("8E")))
EndIf
FileClose($writeFile)

 

8E hex is 142 decimal
AutoIt notes show that chr 142 is  Ž
With the above script:
The message box shows character Ž - good.
ztest in Windows notepad shows Ž - good.
ztest in Windows wordpad shows as Ž - not good.
ztest in Hex editor shows as C5 BD - not good.

The extended character set is supposed to be available to chr().

If I want to write 8E to ztest (not C5 BD) how should I do it then?

Thank you

 

 

Link to comment
Share on other sites

The character saved to the file will be different when you change the file encoding.
You need to be sure about the data you write or read!

- If you write that you want Ž to be recorded as an 8E then use the ANSI encoding.
- If you want to be recorded as C5 BD, then use the encoding that is UTF-8.

UTF-8: Ž = C5 BD     

ANSI:   Ž = 8E     

FileOpen(): $FO_UTF8 (128) /  $FO_UTF8_NOBOM (256) / $FO_ANSI (512)

FileGetEncoding(): Determines the text encoding used in a file.

 

Regards,
 

Link to comment
Share on other sites

#include <FileConstants.au3>
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

Global $DataToWrite = "Ž"
Global $DataToWriteinHex = StringToBinary($DataToWrite, $SB_ANSI)
;~ $SB_ANSI (1) = string data is ANSI (default)
;~ $SB_UTF16LE (2) = string data is UTF16 Little Endian
;~ $SB_UTF16BE (3) = string data is UTF16 Big Endian
;~ $SB_UTF8 (4) = string data is UTF8

Global $iFilePathTest = @ScriptDir & "\zTest.txt"
;~ Local $writeFile = FileOpen($iFilePathTest, $FO_OVERWRITE+$FO_CREATEPATH+$FO_ANSI)
Local $writeFile = FileOpen($iFilePathTest, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY)
;~ $FO_READ (0) = Read mode (default)
;~ $FO_APPEND (1) = Write mode (append to end of file)
;~ $FO_OVERWRITE (2) = Write mode (erase previous contents)
;~ $FO_CREATEPATH (8) = Create directory structure if it doesn't exist (See Remarks).
;~ $FO_BINARY (16) = Force binary mode (See Remarks).
;~ $FO_UNICODE or $FO_UTF16_LE (32) = Use Unicode UTF16 Little Endian reading and writing mode.
;~ $FO_UTF16_BE (64) = Use Unicode UTF16 Big Endian reading and writing mode.
;~ $FO_UTF8 (128) = Use Unicode UTF8 (with BOM) reading and writing mode.
;~ $FO_UTF8_NOBOM (256) = Use Unicode UTF8 (without BOM) reading and writing mode.
;~ $FO_ANSI (512) = Use ANSI reading and writing mode.
If @error Or ($writeFile = -1) Then Exit MsgBox($MB_TOPMOST + $MB_ICONERROR, "Error", "Open File Failured !")
FileWrite($writeFile, $DataToWriteinHex)
FileClose($writeFile)

Global $FileEncoding = FileGetEncoding($iFilePathTest)

MsgBox($MB_TOPMOST + $MB_ICONINFORMATION, "ANSI: 512  >> " & $DataToWriteinHex, "File: " & $iFilePathTest & @CRLF & @CRLF & "FileEncoding=" & $FileEncoding & @CRLF & @CRLF & "FileContent: " & FileRead($iFilePathTest))


Global $DataToWriteinHex = StringToBinary($DataToWrite, $SB_UTF8)
Global $iFilePathTest = @ScriptDir & "\zTest.txt"
Local $writeFile = FileOpen($iFilePathTest, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY)
If @error Or ($writeFile = -1) Then Exit MsgBox($MB_TOPMOST + $MB_ICONERROR, "Error", "Open File Failured !")
FileWrite($writeFile, $DataToWriteinHex)
FileClose($writeFile)

Global $FileEncoding = FileGetEncoding($iFilePathTest)

MsgBox($MB_TOPMOST + $MB_ICONINFORMATION, "UTF8: 256  >> " & $DataToWriteinHex, "File: " & $iFilePathTest & @CRLF & @CRLF & "FileEncoding=" & $FileEncoding & @CRLF & @CRLF & "FileContent: " & FileRead($iFilePathTest))


Exit

 

Regards,
 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...