Convert int to ASCII problem

Hey there!

I have a question regarding my code. I make a simple code to convert an int to ASCII form in the serial monitor. The code work but after the ASCII number printed in the serial monitor there are number '10' that suddenly pop up afterward.

This is my code:

int b;
void setup() {
  Serial.begin(9600);

}

void loop() {
  while(Serial.available() == 0)
  {}
    b = Serial.read();
    Serial.println(b);
}

Thank you and this is my first time post in the forum :slight_smile: .

10 is the ASCII character for newline which is what the ln at the end of println adds ('\n').

If you're sending the serial data to the Arduino from Serial Monitor, you can change the newline behavior of Serial monitor in the dropdown menu at the bottom on the right.

pert:
If you're sending the serial data to the Arduino from Serial Monitor, you can change the newline behavior of Serial monitor in the dropdown menu at the bottom on the right.

I changed the 'newline' to 'no line ending' and it works! Thanks so much.

@OP

From the title of the topics, I understand that you have an integer data like 1234 (for example) which you want to convert into ASCII codes (0x31, 0x32, 0x33, 0x34 = 49, 50, 51, 52) and save them into a suitable variable. We don't see the process of conversion in your sketch other than transferring data from the InputBox of the Serial Monitor to Arduino and then sending back the data to the OutputBox of the same Serial Monitor. If my observation make sense, how do you want to convert your int into ASCII -- using rudimentary codes or C function? Would appreciate to see the post of your efforts?

GolamMostafa:
@OP

From the title of the topics, I understand that you have an integer data like 1234 (for example) which you want to convert into ASCII codes (0x31, 0x32, 0x33, 0x34 = 49, 50, 51, 52) and save them into a suitable variable. We don't see the process of conversion in your sketch other than transferring data from the InputBox of the Serial Monitor to Arduino and then sending back the data to the OutputBox of the same Serial Monitor. If my observation make sense, how do you want to convert your int into ASCII -- using rudimentary codes or C function? Would appreciate to see the post of your efforts?

That's what I want to do, input any integer number to my serial monitor (1, 2, 3, 4, ...) and I want to see if the ouputbox will give ASCII number of that integer number. I don't quite understand the last bit question of yours. Maybe my title misleading about actually converting integer to ASCII, my bad.

rather than locking your loop() waiting for Serial input (in case you would have other things to do), you could write the loop this way:

void loop() {
  if (Serial.available() > 0) {
    int r = Serial.read();
    if (r != -1) { // if read fails, it returns -1,
      Serial.print((byte) r);  // print the byte we received as a decimal value
      Serial.print(" --> ");
      Serial.println((char) r); // print the byte we received as its associated ASCII symbol (sometimes invisible)
     }
   }
}

that will print both the decimal value and show you the ascii character

1 Like

athallar:
That's what I want to do, input any integer number to my serial monitor (1, 2, 3, 4, ...) and I want to see if the ouputbox will give ASCII number of that integer number. I don't quite understand the last bit question of yours. Maybe my title misleading about actually converting integer to ASCII, my bad.

Given the integer number 5678 that you have entered from the InputBox of your Serial Monitor; the converted ASCII codes viz., 0x35, 0x36, 0x37, 0x38 (35363738) will be shown back in the OutputBox of your Serial Monitor. The transformation can be done in two ways : using detailed C codes or using C function. How do you want to do it? Please, show us in the form of post what have you tried.

GolamMostafa:
Given the integer number 5678 that you have entered from the InputBox of your Serial Monitor; the converted ASCII codes viz., 0x35, 0x36, 0x37, 0x38 (35363738) will be shown back in the OutputBox of your Serial Monitor.

hum... not with OP's code

    b = Serial.read();
    Serial.println(b);

--> will see the decimal version of the ASCII characters, one per line so OP will see

[color=purple]53
54
55
56[/color]

when you enter 5678

J-M-L:
when you enter 5678

void setup()
{
  Serial.begin(9600); 
}

void loop() 
{
  if(Serial.available()>0)
  {
    byte x = Serial.read();   //0x35
    Serial.print(x, HEX);
  }
}

SM-3.png

SM-3.png

well if you write code different than what OP has published, then you can say and print whatever you want.. even the phone book...

You used Serial.print(x, [color=red]HEX[/color]); and OP did not... that was just my point