sending a long value using 4 ASCII symbols

Hello.

I’m a beginner with Arduino and I have a little question for whom it may concern.

I am using 2 XBee modules and I would like to send a 32-bits value from one module to the other, using a 4 ASCII symbols package. Tell me if I’m wrong but I think that a ASCII symbol is just an integer between -128 to 127 isn’t it ? So, for example, if I send a ‘a’, I will get a ‘98’ with this code :

char c = ""; //je déclare ma variable c en type char et je l'initialise
for (int i =0; i<256; i++) //un octet complet
  {
    c=char(i); //c prend la valeur du caractère ASCII n° i 
    Serial.println(c); //j'envoie mon caractère sur le port-série
    delay(250); // j'attend 250 ms
  }

.
So I succeeded in sending an integer between -128 an 127 with just one char but I really don’t know how I can send “1000000000” for example.

The reason why I don’t want to send ‘1’,‘0’,‘0’,‘0’…‘0’ and to use a .toInt() function is because I’m limited in baud rate! I want to be as efficient as possible to gain in speed.

If anybody can help me, I would be grateful !

Tanks a lot!

10 0000 0000

can be thought of as 0x200

so you could send '2', '0', '0' and convert it back

Send it as binary. That way any 32 bit integer (or even single precision float) will only take 4 bytes.

When people refer to ascii characters they usually mean human readable characters - from codes 48 to to 126. Using human readable characters has the advantage that they make debugging easier.

This Python function converts a number into 3 ascii chars

def numToChar3(numVal):
        # convert a number into three ascii chars
    if abs(numVal) > 131072:
        numVal = 0
    numVal += 131072
    highChar = int(numVal / 4096)
    midVal = numVal - 4096 * highChar
    midChar = int(midVal / 64)
    lowChar = (midVal - 64 * midChar)
    reslt = chr(highChar + 48) + chr(midChar + 48) + chr(lowChar + 48)
    return reslt

and this Arduino function converts it back into a number

long numParseRcvdChars3(byte ndx) {
 long numVal = 0;
 numVal = (long)(rcvdChars[ndx] - 48) * 4096 + (long)(rcvdChars[ndx+1] - 48) * 64 + (long)(rcvdChars[ndx+2] - 48);
 numVal -= 131072L;
 return numVal;
}

Using 3 characters allows values up to +/- 131072 and, obviously you can extend the concept to use more characters. And, of course, you could implement the equivalent of the Python code on an Arduino. Interestingly the Arduino parse function is very significantly faster than the standard atoi() function.

Another major advantage of using only part of the range of byte values is that you can use some of the other values as start- and end-markers for the message. See the 3rd example in Serial Input Basics. Using start- and end-markers with binary data is more complex.

...R