I am looking for the fastest way to read numerical values stored in binary files.
I have done some functions that seem to work, but I'd like to get a feedback on whether or not my implementation is good.
Here is how I get a signed integer from a 4-bytes little endian block:
signed long int from4li(char const * const buffer)
{
signed long int value = 0;
value += (unsigned char) buffer[3];
value <<= 8;
value += (unsigned char) buffer[2];
value <<= 8;
value += (unsigned char) buffer[1];
value <<= 8;
value += (unsigned char) buffer[0];
return value;
}
This would also work for unsigned integers, but I've originally made a different implementation for unsigned integers (that fails with signed integers, I don't know exactly why):
unsigned long int fromu4li(char const * const buffer)
{
unsigned long int value = 0;
value += (unsigned char) buffer[0] << 8 * 0;
value += (unsigned char) buffer[1] << 8 * 1;
value += (unsigned char) buffer[2] << 8 * 2;
value += (unsigned char) buffer[3] << 8 * 3;
return value;
}
I am more sure about the conversion from an integer to a little endian string buffer, which I think probably couldn't be optimized further:
void to4li(long int const value, char * const buffer)
{
buffer[0] = value >> 8 * 0;
buffer[1] = value >> 8 * 1;
buffer[2] = value >> 8 * 2;
buffer[3] = value >> 8 * 3;
}
I also think that it could be even faster usign memcpy, but to use memcpy I have to know the endianness of the host system.
I don't really want to rely on the endianness of the host system, as I think that my code should be independent of the internal data representation of the host system.
So, is this a proper way of doing those conversions, or can I improve my functions?