About data types

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
十个小矮人
Prole
Posts: 2
Joined: Mon Jun 15, 2020 3:36 pm

About data types

Post 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
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: About data types

Post 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 <)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: About data types

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
十个小矮人
Prole
Posts: 2
Joined: Mon Jun 15, 2020 3:36 pm

Re: About data types

Post by 十个小矮人 »

Thank you

I finished the operation through my friend on the second floor. Thank you
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Bondrusiek, Google [Bot] and 2 guests