Errors in utf-8 encoding
Closed / Works For Mecae6ee6af19d2893839f2861230ae33b2f1a802fCreated 10 years agoUpdated 8 years ago
Descriptionplain2017-05-23 15:57:53
I work under Windows 7, use ActiveTcl 8.6.4.1 for running and Notepad++ for writing TclTk scripts. Script files by default are utf-8 encoded without BOM. A simple program - located (say) in utf8test.tcl - demonstrates the problem.
set fd [open <path_to_file>/utf8test.txt {RDWR CREAT TRUNC}]
binary scan áéíöőúüűÁÉÍÖŐÚÜŰ H* du
puts $fd $du
close $fd; exit
The hexeditor view of the script file
00000000 73 65 74 20 66 64 20 5B 6F 70 65 6E 20 75 74 66 set fd [open utf
00000016 38 74 65 73 74 2E 74 78 74 20 7B 52 44 57 52 20 8test.txt {RDWR
00000032 43 52 45 41 54 20 54 52 55 4E 43 7D 5D 0D 0A 0D CREAT TRUNC}]...
00000048 0A 62 69 6E 61 72 79 20 73 63 61 6E 20 C3 A1 C3 .binary scan áÃ
00000064 A9 C3 AD C3 B6 C5 91 C3 BA C3 BC C5 B1 C3 81 C3 ©Ã.öőúüűÃ.Ã
00000080 89 C3 8D C3 96 C5 90 C3 9A C3 9C C5 B0 20 48 2A ‰Ã.ÖÅ.ÚÜŰ H*
00000096 20 64 75 0D 0A 70 75 74 73 20 24 66 64 20 5C 6E du..puts $fd \n
00000112 24 64 75 0D 0A 0D 0A 63 6C 6F 73 65 20 24 66 64 $du....close $fd
00000128 0D 0A 0D 0A ....
shows that the utf-8 representation of the test string is
C3A1C3A9C3ADC3B6C591C3BAC3BCC5B1C381C389C38DC396C590C39AC39CC5B0
Running the test script by double clicking on utf8test.tcl in the Windows Explorer window, however, produces the the hexadecimal string
c3a1c3a9c3adc3b6c518c3bac3bcc5b1c381c330c38dc313c590c361c353c5b0
Viewing the two string side by side
C3A1C3A9C3ADC3B6 C591 C3BAC3BCC5B1C381 C389 C38D C396 C590 C39A C39C C5B0
c3a1c3a9c3adc3b6 c518 c3bac3bcc5b1c381 c330 c38d c313 c590 c361 c353 c5b0
reveals the problem that I have run up against while developing a much more complex application involving stmp, mime & http.
There is another catch here. Run the script through the wish console. Even if you add 'fconfigure $fd -encoding utf-8' the result is
Q q P p
e1 e9 ed f6 51 fa fc 71 c1 c9 cd d6 50 da dc 70
á é í ö ő ú ü ű Á É Í Ö Ő Ú Ü Ű
You have to apply 'encoding convertto utf-8' to the string then you achieve the desired result. The new script, however, won't work by click-and-run.
You may now draw your conclusions.
Thanks for any help
jan.nijtmansplain2019-02-19 15:52:48
The default "system encoding" on Windows is not UTF-8, so "fconfigure" is indeed the right thing to do.
Log in (anonymous is fine) to add a comment.