Sending an integer serially in Arduino

If what you're looking for is speed, then instead of sending an ASCII encoded int, you can divide your number into two bytes, here's an example:

uint16_t number = 5703;               // 0001 0110 0100 0111
uint16_t mask   = B11111111;          // 0000 0000 1111 1111
uint8_t first_half   = number >> 8;   // >>>> >>>> 0001 0110
uint8_t sencond_half = number & mask; // ____ ____ 0100 0111
    
Serial.write(first_half);
Serial.write(sencond_half);

You don't specify the from environment, so I assume your troubles is with reading serial data on an Arduino?

Anyways, as can be seen in the Arduino Serial reference, you can read an integer using the Serial.parseInt() method call. You can read strings with eg. Serial.readBytes(buffer, length) but your real issue is to know when to expect a string and when to expect an integer (and what to do if something else comes along, eg. noise or so...)


Another way:

unsigned int number = 0x4142; //ASCII characters 'AB';
char *p;
    
p = (char*) &number;
    
Serial.write(p,2);

will return 'BA' on the console (LSB first).