K3fka
Member
So in working on my game, I'm copying a binary file into a buffer. The file can be of a varying size, so I'm doing it one byte at a time until I hit the end. The issue I'm running into now is reading values back from the buffer. The data is all there correctly, but buffer_read() is expecting numbers to be in little-endian and my file is in big-endian. This would presumably be an issue if I wanted to modify the buffer with buffer_write() as well. For now the only workaround I can see if reading the bytes individually and then combining them to get a final value. Is it possible to add a way to read in big-endian?
Edit: In the meantime in case anyone else runs into this issue, here's a quick script I wrote to work around this and switch the endianness of a number
Probably not the most optimal way to do so, but it works.
Edit: In the meantime in case anyone else runs into this issue, here's a quick script I wrote to work around this and switch the endianness of a number
Code:
/// @func switchEndian(number)
/// @desc Converts a number between big- and little-endian
/// @param {int} number
var num = argument0;
var new = 0;
var i;
// Find the max power of 2 to start with
for (i = 0; power(256, i) < num; i++) {
}
// i ends up 1 higher than it needs to be
i--;
// Reverse the process
for (var j = i; j >= 0; j--) {
var temp = num div power(256, j);
num = num mod power(256, j);
new += temp * power(256, i - j);
}
return new;
Last edited: