Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:06:05+00:00 2026-06-07T20:06:05+00:00

when i load data in lua by loadstring , some magic chinese charactors fail.

  • 0

when i load data in lua by loadstring, some magic chinese charactors fail.

RawData = '{a="a朶b"}'
Data = loadstring("return " .. RawData)()

that’s because:

  • “朶” ‘s ascii char(gbk encoding) is 0x96 0x5c
  • 0x5c is ‘\’, which will escape everything afterwards.
  • so, ‘{a=”a朶b”}’ becomes ‘{a=”a\150\b”}’ , \b is the wrong answer

then, i will never get the right output “a朶b” , “b” is eaten by “朶” ….

the same quote problem happens in python:

exec("""print '''a朶b''' """)

there are some ways to handle this in python:

  • clearly define the encoding in file –– coding:gbk ––
  • use utf-8 for string/file encoding

but lua only support standard C, any quote or escaping ideas?


by the way, this works:

RawData = [=[ {a=[[a朶b]]} ]=]
return  loadstring("return " .. RawData)() .a

but i have to change the original RawData, that’s unacceptable.

question 2:

how to keep string in lua not escaped? (python does this job so good)

s  = "a朶b"
s1 = string.format("%q", s)    -- s escaped
return s                       -- s escaped
print(s)                       -- s escaped
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:06:07+00:00Added an answer on June 7, 2026 at 8:06 pm

    As was discussed on the Lua mailing list, Lua handles UTF-8 in string literals just fine. If you can save the file in UTF-8, you will have no problems with Lua. If you need to later use the GBK encoding (like saving it in a file or serving a webpage in the encoding), you can use the lua-iconv library to convert from UTF-8 literals to GBK:

    local iconv = require "iconv"
    local toGbk = iconv.new("GBK", "UTF-8")
    function U2G(s) return toGbk:iconv(s) end
    
    -- assuming the while file is saved in UTF-8, this will output the string in GBK
    print(U2G "a朶b")
    

    The other thing you can do is convert from GBK to UTF-8 before using loadstring. Then do not forget to convert back from UTF-8 when presenting the results to the user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I load data in my code-behind, I find that often I want to
I want to load data written in a variant of lua (eyeonScript). However, the
I need to load some data in my UIViewController to show a UIPickerView widget
How do you load data for a radiogroup in extjs, such that the correct
I'm trying to load data from an external csv file that needs to be
I am trying to load data from XML string into a structure of some
I am trying to load data from a YAML file where some fields contain
I am trying to load some data from a text file into a vector
For an answer to another question , I wanted to load some serialized lua
I want to load data from an XML file and assign that data to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.