Combining two bytes

On the arduino two bytes can be combined with- word(byte, byte); to get a 16 bit int

Is there a way this can be done on the core at the same sizes? I know the data types are different sizes, want to ask before I go on a wild goose chase.

Hi @inof8or

I would do this:

// assume you have byte0 and byte1 as uint8_t or unsigned char types
uint16_t myWord = byte1 << 8 | byte0;  // unsigned
int16_t mySignedWord = byte1 << 8 | byte0;

You just want to avoid the int type in this case since it 32-bits on the Spark core.


Thanks! little bytes help big time

1 Like

Here’s how they do it in Arduino with a macro, compiles on Spark.

#include "application.h"

/* prototypes */
uint16_t makeWord(uint16_t w);
uint16_t makeWord(byte h, byte l);

/* functions */
uint16_t makeWord(uint16_t w) { return w; }
uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8) | l; }

/* macro */
#define word(...) makeWord(__VA_ARGS__)

void setup() {
  uint16_t temp1 = word(255, 255);
  uint16_t temp2 = word(65535);

void loop() {
  // nothing
1 Like