How to convert to unsigned long from 4 char array?
There are two possible answers to this as a processor can be bigendian meaning that the byte with the lowest address is the most significant or littlendian meaning that this byte is the least significant.

I'll assume the the bytes are stored in an array

unsigned char a[4];


and we want the result in

unsigned long retval;


If the system is bigendian

retval  = (unsigned long) a[0] &lt;&lt; 24 | (unsigned long) a[1] &lt;&lt; 16;
retval |= (unsigned long) a[2] &lt;&lt; 8 | a[3]; 


If the system is littlendian

retval  = (unsigned long) a[3] &lt;&lt; 24 | (unsigned long) a[2] &lt;&lt; 16;
retval |= (unsigned long) a[1] &lt;&lt; 8 | a[0];


Note the casts are necessary because otherwise you lose bits.  For example

retval = a[0] &lt;&lt; 8;


Should always be zero assuming that an unsigned char is 8 bit because

a[0] &lt;&lt; 8


Will shift all 8 bits out of the 8 bit variable and they will be lost be lost because the compiler is doing 8 bit arithmetic. The resulting answer 0 will then be written into the 32bit long.

Having the cast changes the number in a[0] into a 32bit value first so the shift does not lose these bits.