Skip to main content

Convert GBK-encoded text to UTF-8 (string.from_gbk)

Declaration

utf8_text = string.from_gbk(gbk_text)

Parameters

  • gbk_text
    string. GBK-encoded text to convert to UTF-8.

Returns

  • utf8_text
    string or nil. UTF-8 text. If conversion fails due to invalid encoding, returns nil.

Description

Convert GBK-encoded text to UTF-8. If the result looks garbled, the input might not be in GBK even though conversion succeeded.
For more advanced conversions, see luaiconv (iconv binding).
Note: GBK includes GB2312, so GB2312 can be converted with this function as well.

Example

-- Chinese encoding GB2312 as subset of GBK
gbkstr = '\x58\x58\x54\x6f\x75\x63\x68\x20\xba\xdc\xc7\xbf'
--
sys.alert(gbkstr) -- GBK text can't be displayed
sys.alert(string.from_gbk(gbkstr)) -- "XXTouch 很强"

Note: Uses function outside of this chapter: sys.alert
Explanation: In Lua string literals, "\xHH" denotes a single byte with hex value HH. For printable chars, see ASCII reference.