This was needed for my Cemu: Zelda , Breath of Wild Trainer.
Figure I could share this snippet of code as I couldnt find anything like this searching the forums and github had some overly complicated code.

I'm pretty sure you meant "big endian." With regards to primitive data types composed of multiple bytes, "little endian" means the least significant byte is stored first (used by x86 architectures) and "big endian" means the most significant byte is stored first (not uncommon to see on emulators).

Code:

32-bit integer:
0x1234ABCD

big endian:
12 34 AB CD

little endian:
CD AB 34 12

_________________

I don't know where I'm going, but I'll figure it out when I get there.

Hm, this looks like it'd only work for 2 byte values (a word in other words)...

If so, why not take 20 minutes and create something like this (note I have not tested this and am no expert on lua but I _think_ it should work lol): https://pastebin.com/hHewwjBT

(same as attached file, I just like providing options)

Note that this reads Big Endian if used on a Little Endian system and Little Endian if used on a Big Endian system since the two are literally just byte reversals and that's what this code (at least theoretically) would do... I suppose if you spent some more time on it that you could determine whether the system was big/little endian (by performing a test if nothing else) and have both big and little endian functions which returned the result they are named after depending on that... but I don't have any real reason to do that

Hm, this looks like it'd only work for 2 byte values (a word in other words)...
I suppose if you spent some more time on it that you could determine whether the system was big/little endian (by performing a test if nothing else)

I dont know what tht means. Just going to use what you found on github now. Thank You for tht._________________

To be clear I found only the reverseTable function on github (I googled since I figured it'd be a common issue even if it was in the standard lua library which I don't really know), the rest I wrote myself (and haven't tested)

As for checking what the system is, something like (again, untested)

Code:

-- get some memory we can write 2 bytes in to test on
local mem = allocateSharedMemory('luaBigEndianTesting', 2)
assert(mem, "Failed to allocate memory to test system endianness!")

-- write a test value
writeWord(mem, 0x0011)

-- set variables to be checked by everything else depending on result
-- might want to make these read-only (metatables? not sure)
-- big endian if most significant byte was written first
systemIsBigEndian = readBytes(mem,1) == 0
systemIsLittleEndian = not systemIsBigEndian -- just an alternative

readIntegerBigEndian = function(address)
if systemIsBigEndian then
return readInteger(address) -- already in correct byte order just read and return it.
else
local bytes = readBytes(address,4,true)
reverseTable(bytes)
return byteTableToDword(bytes)
end
end

Though I can't say for sure whether readInteger would work as shown in the example or if it would always try to read the value as a little endian value even on a big endian system (this would actually be my guess if I had to make one)... either way, you can see that the functions check whether the system is already big endian or not to determine what gets done so that (in theory) the big endian function isn't reversing the bytes when it doesn't need to (thus getting little endian bytes).

Also obviously there could be a third function that always reverses the bytes and then the else sections of the other two would simply return the result of that, purely to reduce code duplication / file size.

Hm, be4 seems to be nil for me in 6.6, though that would also beg the question of whether you can use existing custom types for that purpose or if you'd need to change from using the registry-based custom types to using a lua file in autorun. I did find getCustomType when searching github though it also appears to be nil in 6.6 rather than a function (though github does say the file changed in November and 6.6 was released in October according to the site so not too surprising, it's also not in main.lua for 6.6).

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum