0

How do I read an UTF-8 encoded text file into a text field?

z--
  • 2,186
  • 17
  • 33

2 Answers2

2

When working with Unicode in LiveCode you need to be aware that LiveCode always works with UTF-16 encoded text (format depends on the processor your code is running on: big or little-endian). So to read in a UTF-8 encoded text file and display it you need to first convert the data to UTF-16.

When reading the file in you need to make sure that LiveCode doesn't automatically convert the line endings for you. The simplest way to do that is to use the put command with the URL keyword and a binfile: prefixed filename. Assuming the path to your file is in the variable theFilename this would read in the file contents while maintaining the integrity of the data:

put URL ("binfile:" & theFilename) into theText

Now that you have the data (still encoded as utf-8) in a variable you need to convert to UTF-16 using uniencode.

put uniencode(theText, "utf8") into theText

Now the text is now encoded as UTF-16 and you can display it in a field.

set the unicodeText of field "SomeField" to theText
  • Thank you, Trevor for the concise explanation. I used a test button "Load" with your code – z-- Apr 13 '13 at 05:37
  • `on mouseUp` `answer file "A text file"` `if it <> "" then` `put it into theFileName` `put URL ("binfile:" & theFilename) into theText` `put uniencode(theText, "utf8") into theText` `set the unicodeText of field "SomeField" to theText` `else` `--no file was selected, or cancel was pressed` `beep` `end if` `end mouseUp` – z-- Apr 13 '13 at 05:39
  • http://lessons.runrev.com/spaces/lessons/buckets/809/lessons/12304-How-do-I-use-Unicode-in-Rev- – z-- Apr 19 '13 at 14:50
1

Read up on the following in the dictionary:

useUniCode (property) uniEnCode (function) UniDeCode (function)

And in fact, any entry that has "uni" in its name.

Craig Newman

dunbarx
  • 756
  • 5
  • 4
  • Yes, thank you, Craig. I now see that the uniEncode function in the dictionary has a user contributed comment which answers the question as well. `put url ("binfile:/path/to/file/myUniText.ut8") into tRawTxt` `set the unicodeText of fld "display" to uniEncode(tRawTxt,"UTF8")` – z-- Apr 13 '13 at 05:48
  • see also http://lessons.runrev.com/s/lessons/m/4071/l/6930-how-do-i-use-utf-8-text-with-fields – z-- Apr 19 '13 at 04:10