This should be self explanatory. I want this function to convert 4 bytes into a single long.
Say I call the function as:
void send_data(250, 10, 134, 156, 98);
Noiw the actual function:
void send_data(byte address, byte byte1, byte byte2, byte byte3, byte byte4)
{
byte mybyte[4] = 0;
mybyte[0] = byte1;
mybyte[1] = byte2;
mybyte[2] = byte3;
mybyte[3] = byte4;
// Encode the variables into a 32 bit variable
for ( int i = 0; i < 4; ++i )
{
(reinterpret_cast<char*>(&transmitterdata))[i] = mybyte[i];
}
... now, do whatever with the processed variable...
}
I'm sure this can be simplified. Would it be possible to call the function as:
void send_data(address, myarray[4]);
Thanks
Should the number end up as: 2,501,013,415,698? Your example has 5 bytes, not 4.
Maybe use a union.
union LONGBYTES
{
byte bytes[4];
long longvalue;
};
LONGBYTES data;
void setup() {
data.bytes[0]=3;
data.bytes[1]=2;
data.bytes[2]=1;
data.bytes[3]=0;
Serial.begin(9600);
Serial.println(data.longvalue);
}
Not tested.
The code already converts the 4 bytes to a long, but I would like to pass an array to the function rather than 4 individual bytes. Is this possible?
casemod:
The code already converts the 4 bytes to a long, but I would like to pass an array to the function rather than 4 individual bytes. Is this possible?
hint: the name of the array is a pointer to the first element of the array
something like this:
byte myByte[4] = {10, 134, 156, 98};
void setup()
{
Serial.begin(9600);
send_data(250, myByte); // myByte degrades to a pointer to myByte[0]
}
void loop()
{
}
void send_data(byte address, byte* value)
{
// Encode the variables into a 32 bit variable
for ( int i = 0; i < 4; i++ )
{
Serial.println(value[i]);
//(reinterpret_cast<char*>(&transmitterdata))[i] = value[i];
}
}