发现官网封装的bytearray太强大很多方法用不到,看着头痛,所以自己撸一个够用的
lua本身不提供数据打包支持,quick嵌入lpack。quick文档里没有说明这个我是半天看不懂string.pack string.unpack是哪里来的。
lpack githup地址
https://github.com/LuaDist/lpack
看了个接口文档不是官方的
http://www.luteus.biz/Download/LoriotPro_Doc/LUA/LUA_For_Windows/lpack/
不过文档里面
val, next = unpack( s, F ,init] ) 这个地方搞反了
应该是
next,val = unpack( s, F ,init] )
支持的类型还蛮多的
–
#defineOP_ZSTRING’z’/* zero-terminated string /
#defineOP_BSTRING’p’/ string preceded by length byte /
#defineOP_WSTRING’P’/ string preceded by length word /
#defineOP_SSTRING’a’/ string preceded by length size_t /
#defineOP_STRING’A’/ string /
#defineOP_FLOAT’f’/ float /
#defineOP_DOUBLE’d’/ double /
#defineOP_NUMBER’n’/ Lua number /
#defineOP_CHAR’c’/ char /
#defineOP_BYTE’b’/ byte = unsigned char /
#defineOP_SHORT’h’/ short /
#defineOP_USHORT’H’/ unsigned short /
#defineOP_INT’i’/ int /
#defineOP_UINT’I’/ unsigned int /
#defineOP_LONG’l’/ long /
#defineOP_ULONG’L’/ unsigned long /
#defineOP_LITTLEENDIAN’<’/ little endian /
#defineOP_BIGENDIAN’>’/ big endian /
#defineOP_NATIVE’=’/ native endian */
–]]
–下面是我的封装够用如果不够你在加东西进去 有文档就好办
–ByteArray.lua
Created on 2014年8月22日
@author: YY
可以对数据进行打包,对字节字符串进行解包
–]]
require ‘pack’
local ByteArray = {}
–
@packedStr --已经打包的字节字符串 如果是创建写bytearray 此参数不需要传nil
@endian --字节序 不传默认大字节序
–]]
function ByteArray:new(packedStr, endian)
t = {}
setmetatable(t, self)
self.packedStr =packedStr
self.endian = endian or ‘>’
self.fmt = {}
self.data = {}
self.position = 1
self.__index = self
return t
end
–
清除数据
–]]
function destroy()
self.packedStr = nil
self.fmt = nil
self.data = nil
end
function ByteArray:readByte()
local val = 0
self.position, val = string.unpack(self.packedStr, ‘c’, self.position)
return val
end
function ByteArray:writeByte(b)
self.fmt#self.fmt + 1] = ‘c’
self.data#self.data + 1] = b
end
function ByteArray:readShort()
local val = 0
self.position, val = string.unpack(self.packedStr, self.endian … ‘h’, self.position)
return val
end
function ByteArray:writeShort(h)
self.fmt#self.fmt + 1] = ‘h’
self.data#self.data + 1] = h
end
function ByteArray:readInt()
local val = 0
self.position, val = string.unpack(self.packedStr, self.endian … ‘i’, self.position)
return val
end
function ByteArray:writeInt(i)
self.fmt#self.fmt + 1] = ‘i’
self.data#self.data + 1] = i
end
function ByteArray:readDouble()
local val = 0
self.position, val = string.unpack(self.packedStr, self.endian … ‘d’, self.position)
return val
end
function ByteArray:writeDouble(d)
self.fmt#self.fmt + 1] = ‘d’
self.data#self.data + 1] = d
end
function ByteArray:readString()
local l = self:readShort()
local val = 0
self.position, val = string.unpack(self.packedStr, ‘A’ … l, self.position)
return val
end
function ByteArray:writeString(s)
self:writeShort(#s)
self.fmt#self.fmt + 1] = ‘A’
self.data#self.data + 1] = s
end
–
获取打包后的 数据 -str
–]]
function ByteArray:getbytesString()
if self.packedStr and self.packedStr ~= ‘’ then
return self.packedStr
else
print(self.endian … table.concat(self.fmt))
print(unpack(self.data))
return string.pack(self.endian … table.concat(self.fmt), unpack(self.data))
end
end
return ByteArray
-----------测试
require ‘ByteArray’ --你ByteArray.lua的位置
local function testByteArr()
local ba = ByteArray:new(nil, ‘>’)
ba:writeInt(333300)
ba:writeString(‘fuck 1334 goddess’)
ba:writeShort(44)
ba:writeByte(-3)
ba:writeByte(122)
local s = ba:getbytesString()
print(#s, s)
print(’----------------’)
local ba2 = ByteArray:new(s, ‘>’)
print(ba:readInt())
print(ba:readString())
print(ba:readShort())
print(ba:readByte())
print(ba:readByte())
end
testByteArr()