Page 1 of 1
About data types
Posted: Mon Jun 15, 2020 3:48 pm
by 十个小矮人
Hello, everyone. I have a problem with data types
First of all, thank you for your help
I use love.data.newByteData The (data, 48,4) function gets data of type bytedata
But this data stores an int type or a short type
But I didn't find bytedata to int or to short on the wiki, just found data:getstring () function, but it is not enough to meet my existing needs
Do you have friends who have the same problems,
I hope you can help me
thank you very much indeed
Re: About data types
Posted: Mon Jun 15, 2020 9:43 pm
by pgimeno
I'm not sure I understand what you're asking.
Maybe you want something like
love.data.unpack?
The format strings are specified here:
https://www.lua.org/manual/5.3/manual.html#6.4.2
For example:
Code: Select all
local data = love.data.newByteData('\x91\x92\x93\x94\x95\x96\x97\x98')
local a, b, c, d = love.data.unpack("<Bbi2I4", data)
print(a, b, c, d)
-- output is:
-- 145 -110 -27501 2560071317
-- because:
-- 145 = 0x91, the first byte (format B)
-- -110 is the signed version of 0x92, i.e. 0x92 - 0x100 (format b)
-- -27501 is the signed version of 0x9493, i.e. 0x9493 - 0x10000, a 2 byte signed integer (format i2)
-- 2560071317 = 0x98979695, a 4 byte unsigned integer (format I4)
-- numbers are reversed because it's little endian (format <)
Re: About data types
Posted: Tue Jun 16, 2020 6:34 am
by zorg
You can not handle ByteData contents as specific types with löve functions directly; you need to use the FFI that is provided due to löve using luaJIT itself.
Also, all data is the same, question is how you handle/manipulate the bytes.
Code: Select all
ffi = require 'ffi'
love.data.newByteData(8*4) -- hold 8 4-byte-wide whatevers; could be 4-byte wide integers, could be single prec. floats,
pointer1 = ffi.cast("float*", Data:getFFIPointer()) -- if you want single floats
pointer2 = ffi.cast("int32_t*", Data:getFFIPointer()) -- if you want 32bit ints
pointer3 = ffi.cast("uint8_t*", Data:getFFIPointer()) -- if you want 8bit uints
-- all of the above work; you can even use structs as well. pointers will behave like tables (arrays) with the given type, of course... except they'll be 0-indexed.
Re: About data types
Posted: Tue Jun 16, 2020 1:28 pm
by 十个小矮人
Thank you
I finished the operation through my friend on the second floor. Thank you