converting byte decimal to ascii character

Hello,
I receive int values from serial connection which are the bytes of a number. Lets say i send 123, i ll receive 495051. What i want is to convert this 495051 back to 123. Not print 123 to serial, just convert them in the program so i can do calculations. I searched everywhere including the forum and all that i have found for this topic is how to turn int 53 to char "53" and how to correctly print it to the serial monitor. But nothing of these help me. I tried itoa but it just turns the bytes to char type. If i print sendbuf the characters are still 495051.

Here is a part of my code that shows how i receive the bytes (may have forgotten something because its part of a large code):

#include <SoftwareSerial.h>

static const int RXPin1 = 2, TXPin1 = 3, RXPin2 = 7, TXPin2 = 8, RPin1 = 4, RPin2 = 6;
static const uint32_t GPSBaud = 9600;
int i = 0, j = 0;
int varbuf[32];
char sendbuf[32];
bool received = false;

SoftwareSerial s2(RXPin2, TXPin2);

void setup()
{
   s2.begin(GPSBaud);
}

void loop()
{
Serial.println("Checking for new coordinates...");
delay(1000);
static bool reading = false;
while (s2.available() > 0 && received == false) {
    varbuf[i] = s2.read();
    //itoa(varbuf[i],sendbuf,10);
    if (varbuf[i] == 60) {
      reading = true;
    }
    if (reading) {
      if (varbuf[i] != 60 && varbuf[i] != 62) {
        Serial.print(varbuf[i]);
      }
      if (varbuf[i] == 62) {
        received = true;
        reading = false;
        break;
      }
      i++;
    }
  }
  if (received) {
    Serial.println();
    received = false;
    i = 0;
  }
  else {
    Serial.println("No message");
  }
}

'0' == 0x30 == 4810
....
'9' == 0x39 == 5710

Any help?

TheMemberFormerlyKnownAsAWOL:
'0' == 0x30 == 4810
....
'9' == 0x39 == 5710

Any help?

The program doesn't see it that way. How i make 57 to '9'. I can't do calculations on 57 neither on a char.

Does the ASCII table at the following URL assist you?

char nine = 57;
Serial.print (nine);

I can't do calculations on 57 neither on a char.

Why not?

sirick:
The program doesn't see it that way. How i make 57 to '9'. I can't do calculations on 57 neither on a char.

char c = '9';
int i = c - '0';
if (i == 9)
{
  //....
}

This code is plainly nonsense...

if (varbuf[i] != 60 && varbuf[i] != 62) {

varbuf[​i] cannot possibly be both 60 ('<') and 62 ('>') at the same time.
Perhaps you meant OR (||) and not AND (&&)?

Also why not use the character literals '<' and '>' instead of ASCII codes. It will save you the effort of lookng them up, and save us reaching for the ASCII table to decipher your code.

sirick:
I receive int values from serial connection which are the bytes of a number. Lets say i send 123, i ll receive 495051. What i want is to convert this 495051 back to 123.

1. The following Table of Fig-1 shows the ASCII codes (in hex base) for the printable characters of the English Language (a - z, A - Z, 0 - 9, special characters like $ and others, punctuation marks like ! and others).


Figure-1:

2. Save the received ASCII codes in the following array. In fact, you are receiving 0x31, 0x32, and 0x33 and not 495051 which are the images/values that can be seen on the OutputBox of Serial Moniotr in response to print() command.

char myData[4];    //it will hold 0x31, 0x32, 0x33, and null character (00x00)

3. Now, extract 1 from the content of myData[0]; extract 2 from the content of myData[1]; extract 3 from the content of myData[2].

byte x0 = myData[0] - 0x30;    //01
byte x1 = myData[1] - 0x30; //02
byte x2 = myData[2] - 0x30; //03

4. Manipulate result of Step-3 by shifting and adding/ORing to get 123.

int x = x1<<4 | x2;    //x = 0010000 (0x20) + 00000011 (0x03) = 0x23
int y = x0<<8 |  x; //y = 0x010000 + 0x0023 = 0x0123
Serial.print(y, HEX);  //shows this image: 123

5. All the works of Step-3, 4 could be done by the following single code using the atoi() function:

int z = atoi(myData);  //z = 0x7B in hex = 123 in decimal
Serial.print(z, DEC);    //shows: 123

But if you follow @GolamMostafa's advice, make sure "myData" is static, or a global.

TheMemberFormerlyKnownAsAWOL:
'0' == 0x30 == 4810

'0' == 3016 == 4810??

pcbbc:
This code is plainly nonsense...

if (varbuf[i] != 60 && varbuf[i] != 62) {

varbuf cannot possibly be both 60 ('<') and 62 ('>') at the same time.
Perhaps you meant OR (||) and not AND (&&)?[/quote]
I think you overlooked the '!='. This is like:
* *if (not (varbuf[i] == 60 or varbuf[i] == 62) ) {* *

GolamMostafa:
'0' == 3016 == 4810??

Am I missing something here?

TheMemberFormerlyKnownAsAWOL:
Am I missing something here?

I was looking for uniformity.

GolamMostafa:
1. The following Table of Fig-1 shows the ASCII codes (in hex base) for the printable characters of the English Language (a - z, A - Z, 0 - 9, special characters like $ and others, punctuation marks like ! and others).


Figure-1:

2. Save the received ASCII codes in the following array. In fact, you are receiving 0x31, 0x32, and 0x33 and not 495051 which are the images/values that can be seen on the OutputBox of Serial Moniotr in response to print() command.

char myData[4];    //it will hold 0x31, 0x32, 0x33, and null character (00x00)

3. Now, extract 1 from the content of myData[0]; extract 2 from the content of myData[1]; extract 3 from the content of myData[2].

byte x0 = myData[0] - 0x30;    //01

byte x1 = myData[1] - 0x30; //02
byte x2 = myData[2] - 0x30; //03




**4.** Manipulate result of Step-3 by shifting and adding/ORing to get 123.


int x = x1<<4 | x2;    //x = 0010000 (0x20) + 00000011 (0x03) = 0x23
int y = x0<<8 |  x; //y = 0x010000 + 0x0023 = 0x0123
Serial.print(y, HEX);  //shows this image: 123




**5.** All the works of Step-3, 4 could be done by the following single code using the atoi() function:


int z = atoi(myData);  //z = 0x7B in hex = 123 in decimal
Serial.print(z, DEC);    //shows: 123

That helped me and code worked! Thank you very much! And the rest of you for the explanations.

GolamMostafa:
I was looking for uniformity.

It is uniform.

ascii zero.png

ascii zero.png

GolamMostafa:
I was looking for uniformity.

I was looking for clarity.

What is the maximum int an arduino uno can receive? If i send 12345, it prints correctly but if go above that (123456), atoi won't work. It will print a random number. So i suspect that the buffer overloads?

int can be -32768 to 32767
unsigned int can be 0 to 65535

long & signed long are 32 bits if need 6 decimal digits

sirick:
What is the maximum int an arduino uno can receive? If i send 12345, it prints correctly but if go above that (123456), atoi won't work. It will print a random number.

] I doubt the number will be random.

TheMemberFormerlyKnownAsAWOL:
] I doubt the number will be random.

value = atoi(sendbuf);
Serial.println(value);
Serial.println(sendbuf);

first print:-7616
second print:123456

same happens if i add more digits

Use strtol() or strtoul().