今天在使用Xlua修改加载lua文件方式的时候碰到了lua文件第一行报错的问题。后来发现是由于Xlua加载使用字节流的方式,而自己的lua文件编码方式选择的是UTF-8 BOM导致的。
什么是BOM?
BOM: Byte Order Mark
UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支持UTF-16,UTF-32才加上的
BOM,BOM签名的意思就是告诉编辑器当前文件采用何种编码,方便编辑器识别,但是BOM虽然在编辑器中不显示,但是会产生输出,就像多了一个空行。
Byte Order Marks are special characters at the beginning of a Unicode file to indicate whether it is big or little endian, in other words does the high or low order byte come first. These codes also tell whether the encoding is 8, 16 or 32 bit. You can recognise Unicode files by their starting byte order marks, and by the way Unicode-16 files are half zeroes and Unicode-32 files are three-quarters zeros. Unicode Endian Markers
Byte-order mark Description
EF BB BF UTF-8
FF FE UTF-16 aka UCS-2, little endian
FE FF UTF-16 aka UCS-2, big endian
00 00 FF FE UTF-32 aka UCS-4, little endian.
00 00 FE FF UTF-32 aka UCS-4, big-e

本文介绍了在使用XLua时遇到的由于Lua文件采用UTF-8 BOM编码导致的加载错误问题。BOM用于标识文件编码,但在UTF-8中并非必需,反而可能引起解析错误。通过理解BOM在不同编码中的作用,以及其在UTF-8中的特殊性,可以解决此问题。文中提到的解决方案是在Unity Editor中创建工具,自动检测并转换Lua脚本为无BOM的UTF-8编码。
最低0.47元/天 解锁文章
1657

被折叠的 条评论
为什么被折叠?



