Combining two unsigned ints size length n into one length 2n

Hey,
so I'm trying to combine two Uint_16 sensor readings into one integer value so they can be transferred in one Tx transmission. Eg. 100 and 101 would turn into 100101.

I'm using value=(100*1000)+101; at the moment but feel as if there is a much more efficient way to do this, I know in VHDL " &" can be used to do this. does anyone know of any functions that do this better? I'm going to try pointers in the time being.

Thanks

RTAIGH:
Hey,
so I’m trying to combine two Uint_16 sensor readings into one integer value so they can be transferred in one Tx transmission. Eg. 100 and 101 would turn into 100101.

I’m using value=(100*1000)+101; at the moment but feel as if there is a much more efficient way to do this, I know in VHDL " &" can be used to do this. does anyone know of any functions that do this better? I’m going to try pointers in the time being.

Thanks

Hello

Are the values binary or decimal values?

decimal vaules would be
dst=(upper_value * 1000) + lower_value;

binary values would be
dst=(upper_value << 3) + lower_value;

that is assuming that the binary values are less than 8. i.e. 000 through 111

another way would be to create a character string.
dst=“upper_value | lower_value”;

the pipe charactor ‘|’ acts as the delimiter.

RTAIGH:
so I’m trying to combine two Uint_16 sensor readings into one integer value so they can be transferred in one Tx transmission. Eg. 100 and 101 would turn into 100101.

You need to think a bit more carefully about this.

If it is the case that each of the Uint_16 memory slots actually only has an 8-bit number then you can combine them like this

combinedInt = (highValue << 8) + lowValue.

But the new number will be meaningless in an arithmetic sense unless you extract the two parts from it later on.

…R

RTAIGH:
Hey,
so I’m trying to combine two Uint_16 sensor readings into one integer value so they can be transferred in one Tx transmission.

What type of Tx? Most transmissions send bytes (or even bits at low level), not 4 bytes in one go

If you have an API to send multiple bytes in one call, such as Serial.write(buf, len) and want to keep it simple create a struct with your two integral values and send the struct

struct {
   uint16_t v1;
   uint16_t v2;
} sensorsValues;

...
sensorsValues.v1 = 100;
sensorsValues.v2 = 101;
...
Serial.write((byte*) &sensorsValues, sizeof(sensorsValues));