-- 判断GB2312汉字
function IsAllChinese(str)
local nStrCount = string.len(str)
if math.mod(nStrCount, 2) ~= 0 then
return false
end
for i = 1, nStrCount-1, 2 do
local nCurByte = string.byte(str, i)
local nNextByte = string.byte(str, i+1)
if not IsChineseCharacter(nCurByte, nNextByte) then
return false
end
end
return true
end
local nNum1Min = tonumber("0XB0", 16)
local nNum1Max = tonumber("0XF7", 16)
local nNum2Min = tonumber("0XA1", 16)
local nNum2Max = tonumber("0XFE", 16)
-- 判断字符是否是中文
function IsChineseCharacter(nCurByte, nNextByte)
if not (nCurByte >= nNum1Min and nCurByte <= nNum1Max) then
return false
end
if not (nNextByte >= nNum2Min and nNextByte <= nNum2Max) then
return false
end
return true
end
原理参照我写的两片字符编码的博客,看完就对字符编码了然于胸。