two consecutive chars to one int.

Quick question:
Is there an easy way to read two consecutive chars or bytes from a serial buffer and combine them into one integer?
For example, char 1 and char 2 would be read until a '/' was read then combined to make 12.

I'd like to avoid using a String if possible.

Thanks!

If I understand the problem, this might lead you to an answer:

void setup() {
  int answer;
  char myChar[3];

  Serial.begin(9600);
  
  myChar[0] = '1';      // Actually use Serial.read() for these and get it from keyboard or wherever...
  myChar[1] = '2';
  myChar[2] = '/';

  if (myChar[2] == '/')
    myChar[2] = '\0';   // Make it a C string

  answer = atoi(myChar);
  Serial.println(answer);
}

void loop() {
}

Your question was too vague. Did you want what @econjack posted? Or, did you mean that you want to pack two 8-bit integers into a single 16-bit integer. If the latter, some folks prefer the ‘union’ method. I think the method below is a little more portable as you don’t have to worry about endianness.

void setup() {
 Serial.begin(115200);
 delay(2000);

 uint8_t hiByte = 0x12;
 uint8_t loByte = 0x34;
 uint16_t combined = ((uint16_t) hiByte << 8) | loByte;
 Serial.println(combined, HEX);
}

void loop() {
}

gfvalvo:
Your question was too vague. Did you want what @econjack posted? Or, did you mean that you want to pack two 8-bit integers into a single 16-bit integer. If the latter, some folks prefer the ‘union’ method. I think the method below is a little more portable as you don’t have to worry about endianness.

void setup() {

Serial.begin(115200);
delay(2000);

uint8_t hiByte = 0x12;
uint8_t loByte = 0x34;
uint16_t combined = ((uint16_t) hiByte << 8) | loByte;
Serial.println(combined, HEX);
}

void loop() {
}

Actually, this might be preferable:

void setup() {
	Serial.begin(115200);
	delay(2000);

	uint8_t hiByte = 0x12;
	uint8_t loByte = 0x34;
	uint16_t combined = hiByte;
	combined = combined << 8 | loByte;
	Serial.println(combined, HEX);
}

void loop() {
}

Thank you for the replies. I did figure out a solution and it seems to be working although it may not be all that elegant.

 while(time.available() > 0) {
    result[slot] = time.read();
    slot ++;   
  }

  month  = (10 * (result[0]  - '0')) + (result[1]  - '0');
  day    = (10 * (result[3]  - '0')) + (result[4]  - '0');
  year   = (10 * (result[6]  - '0')) + (result[7]  - '0');

  hour   = (10 * (result[9]  - '0')) + (result[10] - '0');
  min    = (10 * (result[12] - '0')) + (result[13] - '0');
  second = (10 * (result[15] - '0')) + (result[16] - '0');

The problem comes from grabbing date and time from the linux side of the yun. The yun replies with
"12/15/17-05:15:47"

I wanted to take all those values and save them to uint8_t's. I made an array of 16 chars named result[16] and saved the whole response to them and then took the slots I needed, converted them to numbers and then added them together. It works well. Any ideas on how to make this solution more elegant are welcome!

Now that I read my original post I can see how it's vague. char 1 would be read as '1' and char 2 would be read as '2'. Combining them into an int would make the value of 12.