3 ms·
But now he needs 2 variables, i and data, while otherwise I can just read in i and swap it afterward on a big-endian machine (assuming I read in little-endian c
by coffeeaddicted 15y ago
But now he needs 2 variables, i and data, while otherwise I can just read in i and swap it afterward on a big-endian machine (assuming I read in little-endian certainly).
- yason 15y agoYou need two variables anyway to make any sense into the code. Where would you get the value of 'i' if you didn't have 'data' and what would you do with the value read from 'data' if you didn't have 'i' or some equivalent?
- coffeeaddicted 15y agoI don't need a 'data' variables when I read directly from file. For example: "fread( (void *)&i, sizeof(i), 1, file);". Works also with (packed) structs. For the solution as given in the article I need now a second variable 'data' first to buffer the information I read from file. edit: I mention this case as it covers around 90% of the cases I've seen on a quick check over a codebase I'm working with (Irrlicht). Swapping endian is nearly always done after reading in the data from a file-stream.
- alexchamberlain 15y agoMultiple variables shouldn't be scary. Storing the same data multiple times is... You just need to use pointers. See https://github.com/alexchamberlain/byte-order https://github.com/alexchamberlain/byte-order.
- coffeeaddicted 15y agoYou realize your code is littered with just those defines which the article wrote are not necessary at all? You are exactly proving my point, you don't need a second variable in the case where you don't need to switch bytes when you use a define. And the trick is to use the define only after you already have the value already in the integer. In his solution you would need the data pointer which you have put into the define _always_.
- alexchamberlain 15y agoThey are necessary to provide optimal code without relying on the optimiser.