sneakyimp;10960718 wrote:
I've seen htonl/ntohl mentioned as possibilities but it's my understanding that these are a) not always defined or available
But you can easily check this with #if !defined and then proceed to define them yourself.
sneakyimp;10960718 wrote:
b) limited to 32-bit unsigned integers.
(...)
I'm worried now about doubles. That's the reason for my other post. I'm guessing that to be thorough, I need to find out how many bytes are involved
Ok, I've never had to deal with this myself, but since all data is stored as bytes, it seems to me that you'd be able to use the exact same approach to revers endianness for anything, wether it's int, float or UTF-8 characters.
A double is an 8 byte float (according to IEEE 754-2008, also see wikipedia entry for double). As far as endianness goes, wether it's 8 bytes or something else should not matter (except for exactly how the reversal algorithm needs to be constructed). But assume 8 bytes for the sake of an example, and just think about how a decimal number is stored, i.e.
1 bit sign
11 bit exponent
52 bit fraction
1/3 would be represented as 0x3fd5 5555 5555 5555 which means that memory inspection on BE systems would show exactly that. I never did much C, and it's a long long time ago, but on an LE system, the following
double d = 1.0/3;
void *i = &d;
for (int j = 0; j < (sizeof(double) / sizeof(char)); ++j) {
printf("%x ", *( (char *) i) + (j * sizeof(char)) ));
}
ought to show
55 55 55 55 55 55 d5 3f
What the code does (or is supposed to do anyway), is to store the value of floating point division 1/3, and then step through it one byte at the time to inspect the byte ordering.
The conclusion is that even though you are no longer dealing with integers, all you need to know is the size of what you store, and then perform your "reverse endianness" accordingly.
"Size of what you store" would of course refer to the underlying data type. I.e. for a string it's not the size of the string, but rather the size of the characters: either single bytes (iso 8859-1) or multiple bytes (utf-8).
sneakyimp;10960718 wrote:
and to be safe I need to avoid writing any adjacent bytes should I need to cast a double as a char pointer.
I don't doubt you, but I don't see the problem myself, and I wouldn't mind an explanation.
sneakyimp;10960718 wrote:
I'm not sure what this means? Could you clarify? Do you mean "no unsigned integer in PHP?"
Ah yes, unsigned. And thinking about it, I suppose that shouldn't pose any problems for you.
sneakyimp;10960718 wrote:
Criminy! It never ends.
Hehe. But personally I wouldn't worry about this (but perhaps that's why I don't write extensions).
At least not initially. If you create functions to handle BE/LE and pass your doubles, ints etc through these, all you have to do is add the mixed endian handling there later down the road if you decide it's needed.
And at the very least I'd start by finding out what system(s) use this. If there are any, and if they are still in use, also find out if PHP even compiles on them...