typecasting

This is a discussion on typecasting within the C++ Programming forums, part of the General Programming Boards category; hello! how can i convert unsigned char a[4] to unsigned long. Point is that i need to convert pixel, wich ...

The third way uses shifts (this is the one where you can control endianness):

Code:

unsigned long l = a[0] | a[1] << 8 | a[2] << 16 | a[3] << 24;

In all cases, remember that on my Athlon64, gcc will have 8-byte longs, so the result might not be what you expect. If your compiler has it, include <stdint.h> and use uint32_t instead. This header is part of the C99 standard. If it doesn't have it, or you just want to be portable, include <boost/cstdint.hpp> from the Boost libraries. It defines the same types, but in the boost namespace.