中文字符的整数形式大于127
2015-08-10 21:11
197 查看
-- 计算字符串宽度
local
str =
"Jimmy: 你好,世界!"
local
fontSize = 20
local
lenInByte = #str
local
width = 0
for
i=1,lenInByte
do
local
curByte =
string.byte
(str, i)
local
byteCount = 1;
if
curByte>0
and
curByte<=127
then
byteCount = 1
elseif
curByte>=192
and
curByte<223
then
byteCount = 2
elseif
curByte>=224
and
curByte<239
then
byteCount = 3
elseif
curByte>=240
and
curByte<=247
then
byteCount = 4
end
local
char =
string.sub
(str, i, i+byteCount-1)
i = i + byteCount -1
if
byteCount == 1
then
width = width + fontSize * 0.5
else
width = width + fontSize
(char)
end
end
(
"总宽度: "
..width)
相关文章推荐
- pfx,cer转pem,并对通过pem文件进行签名与验签
- printf char*问题
- 遍历三维数组
- HDU 4233 Family Fortune 优先队列
- java复习-类和对象(一)
- Android Synchronization Fences – An Introduction
- Android之事件传递机制
- HDU 1879--继续畅通工程【kruskal && 最小生成树 && 水题】
- 8.10学习总结
- 消息机制4
- Bad owner or permissions on $HOME/.ssh/config
- 遍历三维数组
- PostgreSQL中流复制pg_basebackup做了什么
- 2.17——数组循环移位
- 线性表的顺序存储结构
- OpenCV入门学习(三)HistogramEquivalent
- mac install brew
- android开发中遇到的问题汇总【八】
- hdu1512-Monkey King- 左偏树+并查集
- 数据库