Old 03-25-2019, 07:33 PM   #1
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default Is it possible to change the file encoding in lua?

I created a script to write some information to a csv file. Then I realized that the file is encoding in utf-8. I need to convert it to ANSI. Now I have to open it with windows built in notepad and re-save it as ANSI.

I'm looking for a way to save to ANSI directly in lua, skipping the notepad. Is it possible?
dsyrock is offline   Reply With Quote
Old 03-27-2019, 09:46 AM   #2
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,628
Default

You could try fiddling around with the utf8-module-functions. Though I don't know, how to do that exactly for conversion stuff.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 03-27-2019, 09:57 AM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

You could try: https://stackoverflow.com/questions/...ii-in-pure-lua

I'm surprised that your script is saving the data as UTF-8 though, since Lua doesn't use it by default.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 03-27-2019, 08:37 PM   #4
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default

Quote:
Originally Posted by Lokasenna View Post
You could try: https://stackoverflow.com/questions/...ii-in-pure-lua

I'm surprised that your script is saving the data as UTF-8 though, since Lua doesn't use it by default.
I'm not sure. I just simply using io.output to creat a csv file. Is it because reaper forcing everything output from itself as UTF-8 by default?
dsyrock is offline   Reply With Quote
Old 03-27-2019, 09:03 PM   #5
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default

Quote:
Originally Posted by Lokasenna View Post
You could try: https://stackoverflow.com/questions/...ii-in-pure-lua

I'm surprised that your script is saving the data as UTF-8 though, since Lua doesn't use it by default.
I sort of found out the reason lua save data as utf-8. When the data contain any non-English words, in my case Chinese, then it will change it to utf-8.
dsyrock is offline   Reply With Quote
Old 03-28-2019, 09:24 AM   #6
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,628
Default

Hmm...some sort of control over the encoding would probably better.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 03-28-2019, 04:51 PM   #7
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,964
Default

REAPER uses UTF-8 so all strings obtained from the API are UTF-8. Lua string literals (which are just arrays of bytes) are encoded as they are written in the script (so if the script file itself is UTF-8 you'll get UTF-8 out). Which ANSI codepage are you targeting? (looks like there's a few chinese ones)

Last edited by cfillion; 03-29-2019 at 12:08 AM.
cfillion is offline   Reply With Quote
Old 03-28-2019, 11:13 PM   #8
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default

Quote:
Originally Posted by cfillion View Post
REAPER uses UTF-8 so all strings obtained from the API are UTF-8. Lua string literals are encoded as they are written in the script (so if the script file itself is UTF-8 you'll get UTF-8 out). Which ANSI codepage are you targeting? (looks like there's a few chinese ones)
I'm using GB18030
dsyrock is offline   Reply With Quote
Old 03-01-2021, 09:22 AM   #9
BartR
Human being with feelings
 
BartR's Avatar
 
Join Date: Oct 2014
Location: Belgium
Posts: 1,622
Default

Quote:
Originally Posted by Lokasenna View Post
You could try: https://stackoverflow.com/questions/...ii-in-pure-lua

I'm surprised that your script is saving the data as UTF-8 though, since Lua doesn't use it by default.
I just got the confirmation now, that the text file I was exporting in LUA is UTF8 and not ANSI. I need to solve this as well.
On my case I just use things like that

Code:
local variable = "ààà"
file:write(variable)
but it writes it in UTF8 not in ANSI and I have not clue how to convert it within LUA
__________________
Reaper: always the most up-to-date.
O.S.: Windows 11 Pro
ReaPack (with bilingual Tutorials): https://bit.ly/ReaPack_Repository
BartR is offline   Reply With Quote
Old 03-01-2021, 03:14 PM   #10
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,964
Default

The string literal contains UTF-8 because the text editor (REAPER IDE?) encoded it as such. Writing a string variable to a file in Lua copies the bytes as-is.

You can specify characters using their byte value in any encoding, without having to change the encoding of the script itself:

Code:
local variable = "\xc3\xa0\x0a" -- utf8 for à (3 bytes)
Just curious, why do you need to downgrade from the universal UTF-8 to a system-specific ANSI codepage?

Last edited by cfillion; 03-02-2021 at 06:06 PM.
cfillion is offline   Reply With Quote
Old 03-02-2021, 04:45 AM   #11
BartR
Human being with feelings
 
BartR's Avatar
 
Join Date: Oct 2014
Location: Belgium
Posts: 1,622
Default

Quote:
Originally Posted by cfillion View Post
The string literal contains in UTF-8 because the text editor (REAPER IDE?) encoded it as such. Writing a string variable to a file in Lua copies the bytes as-is.

You can specify characters using their byte value in any encoding, without having to change the encoding of the script itself:

Code:
local variable = "\xc3\xa0\x0a" -- utf8 for à (3 bytes)
Just curious, why do you need to downgrade from the universal UTF-8 to a system-specific ANSI codepage?
For a script I have made (ChapterRegion.lua) that exports a sidecar TXT file, readable by some radio-automation software based on ANSI (accented letters were totally screwed-up)

BUT the good news is: after have told to the company what the issue is, and that it will be much better for them to switch from ANSI t UTF-8, they answered to me just few minutes ago, that from next releas on, they will read the SideCars in UTF8.

They have tested on a sample I sent them, and everything is now fine.

So: problem solved to the root :-)
__________________
Reaper: always the most up-to-date.
O.S.: Windows 11 Pro
ReaPack (with bilingual Tutorials): https://bit.ly/ReaPack_Repository
BartR is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.