Friday, June 23, 2017

#1 2005-10-21 07:43:54 am

julifos
Administrator
From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

Can I read/write text as UTF-16?

Yes. You can append a little "as Unicode text" (built-in class in AppleScript, which is UTF-16) to your read-write commands and you are done.

Applescript:

set f to (open for access file x)
set eof of f to 0 --> empty file contents if needed
write "foo" to f as Unicode text
close access f

If you need this file into a text editor, we would recommend adding a little flag to the file (called BOM: Byte Order Mark), so the text editor recognizes this is a utf-16 encoded file:

Applescript:

set f to (open for access file x)
set eof of f to 0 --> empty file contents if needed
--> now write the flag
write ((ASCII character 254) & (ASCII character 255)) to f --> not as Unicode text
--> now write the data
write "foo" to f as Unicode text
close access f

We wrote 0xFE and 0xFF (in hex notation) as BOM, which is the rigth mark in Mac, while in a PC we would swap this as 0xFF and 0xFE (as well as all the other bytes).

Reading is very similar:

Applescript:

read file x as Unicode text

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.035 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)