Sounds like a job for a union.
union ArrayToInteger {
byte array[4];
uint32_t integer;
};
void setup(){
Serial.begin(115200);
ArrayToInteger converter; //Create a converter
converter.array[0] = 1 ; //save something to each byte in the array
converter.array[1] = 1 ; //save something to each byte in the array
converter.array[2] = 1 ; //save something to each byte in the array
converter.array[3] = 1 ; //save something to each byte in the array
Serial.println(converter.integer); //Read the 32bit integer value.
}
void loop(){
}
This would convert the array {1,1,1,1} into the long integer 16843009.
Basically the union defines a type which is made up in this case of a uint32_t member called 'integer' and a member which is an array of four bytes. The clever thing about unions is, both these two variables share the same memory space, which means no conversion is necessary - if you change one, the other changes to match.
Interestingly, you can also use array constructors with this (same code as before, but simplified):
union ArrayToInteger {
byte array[4];
uint32_t integer;
};
void setup(){
Serial.begin(115200);
ArrayToInteger converter = {1,1,1,1}; //Create a converter
Serial.println(converter.integer); //Read the 32bit integer value.
}
void loop(){
}