converting int or byte to ascii

This way I am converting int to ascii if its between 0-9 ascii number.

Serial.write((char)id+48);

but is there easy way to deal with int whose value is >128?

lets say if int id=200.

Serial.print is not working in my case. I need serial.write.

Also tried using modulo in for loop its working but may be shorter way is possible.
thanks.

this is my code with modulo and for loop.

unsigned int id=256;
unsigned int _size=id;
byte i=0;
while(_size>0){
  _size/=10;
  i++;
  }
byte number[i-1]={0};

for(byte i=(unsigned int)(sizeof(number))/sizeof(number[0]);i>=0 && id>0;i--){
  number[i]=id%10;
  id/=10;
  }
  for(byte i=0;i<=(unsigned int)(sizeof(number))/sizeof(number[0]);i++){
    Serial.write((char)(number[i]+48));}
  }

Where to start?

First ASCII values are 0-127 and anything above 127 is OUTSIDE the ASCII range.

See the ASCII chart at http://en.cppreference.com/w/cpp/language/ascii

The values are further partitioned between control characters, alpha characters, digits, punctuation and several types of space characters.

Look up the functions isupper() or islower(), iscntrl(), isdigit(), ispunct() and isspace() to make a determination of which partition the id is a member of first.

Once partitioned into type your example for a digit would be

Serial.write('0' + id);

think I didnt explained clearly the problem.

int id is holding value 256 i.e. if represented in ascii form it must be 50,53,54.

in other words int value 256 must be written via serial.write to the screen. As serial.write cant write 256 I converted to be able to write to the screen.

But in my opinion code is too big for that small problem. So I am wondering if there is a shorter way ?

in my case id is always a number. if id=48 then in ascii must be 52,56 but not char '0'.

Let us assume that your id is:

int id = 256;

What do you want now?

Do you want to see this figure(256) as it is on the Serial Monitor?

Do you know that Serial Monitor is an ASCII display unit? ASCII display unit means -- if we want to see a character (A to Z, a -z, 0 - 9, punctuation marks) on the Serial Monitor, we need to send to the Serial Monitor the ASCII code of that character. Thus --

1. to see 2, we need to send: 0x32 --> Serial.write(0x32); --> Serial.write (50); --> Serial.write(2 + 0x30);--> Serial.write(2 + 48);

2. to see 5, we need to send: 0x35 --> Serial.write(0x35); --> Serial.write (53); --> Serial.write(5+ 0x30); --> Serial.write(5 + 48);

3. to see 6, we need to send: 0x36 --> Serial.write(0x36); --> Serial.write (54); --> Serial.write(6 + 0x30); --> Serial.write (6 + 48);

Now, the question comes -- how to show the digits of an unknown id of type integer (4-digit id : 0000 - 9999). We need to write program for it?

surepic:
int id is holding value 256 i.e. if represented in ascii form it must be 50,53,54.

int d = 256;
Serial.print(d);

::slight_smile:

256 must be written via serial.write to the screen.

Why the need to use Serial.write() ?

@GolamMostafa
My function is doing exactly what you wrote its separating digits from numbers then doing conversion. I was searching for a shorter way of doing same thing.

I cant use serial.print . I would like to but with serial.print im getting not all chars on the other side. Only serial.write is working. Its not a screen its esp8266 and it accepts ascii only.

xyproblem - the problem is something else, not a bug in Serial.print I think...

You’re not making a lot of sense.

Can you provide examples where you think print isn’t working for you.

No need to write the whole code i did it already in one of my other posts.

Simple code like this is not working:

Serial.begin(115200);
Myserial.begin(115200); //softserial

And in loop
Myserial.println(“AT”); //to send with \r\n

Esp is getting only ‘A’ or sometimes unknown char after ‘A’.

With speed of 9600 things are getting even worse. It may not get anything.

With serial write no problem. So all my strings im breaking in loop to send byte by byte with 1ms delay. Without 1ms delay again not always chara are getting to esp. That way even string with 200chars is getting to the destination.

For logic converting i build small curcuit with n-type mosfets.

Tried without softwareserial directly to rx and tx feedback via led. Same problem write works println doesnt.

Again if the whole code is needed can post here.

Its not a screen its esp8266 and it accepts ascii only.

That means that you want transfer int id = 256 (for example) from UNO to ESP over UART Port using minimum number of instructions. Is it Correct?

Exactly! But not const 256. Any number with length up to 3. I.e. 1-999

Can you please post a simple but complete program that sends the data and a short but complete program that receives the data that shows the problem.

#include <SoftwareSerial.h>

#define max_attempts 15
#define input_buffer_size 250
#define restart 1
const char RST PROGMEM = “AT+RST\r\n”;

byte receive_command(char ans);
byte send_command(byte command);
bool esp_init(void);
SoftwareSerial mySerial(10,11);

void setup() {
Serial.begin(115200);
while (!Serial);
mySerial.begin(115200);

while (!esp_init());

Serial.println(“everything went ok”);
}

void loop(){}

bool esp_init(void) {
bool success = 1;
success &= send_command(restart);
delay(1000);
return success;
}
receiving function.

byte receive_command(char ans[]) {
  char c = 0;
  char a[input_buffer_size] = {0};
  byte b = 0;
  unsigned long _time = 0;
  const int max_wait_time = 10000;
  _time = millis();
  byte i = 0;
  
  while (!mySerial.available()) {
    if ((millis() - _time) > max_wait_time) return b;
  }
  
  while ((c = mySerial.read()) != '\n') {
    if (c >= 32 && c <= 127) { // check if ascii else discard
      a[i] = c;
      i++;
    }
  }
  
  a[i + 1] = '\0';
  if (b = strstr(a, ans)) b = 1;
  
  return b;
}

sending function. in this function I didnt get into that ID thing but here I`m using only write cos println is just not working.

byte send_command(byte command) {
  byte timout = 10;
  bool text_response = 0;
  char _command[100] = {0};
  char ans[] = "OK";
  switch (command) {
    case restart:
      for (byte i = 0; i < strlen_P(RST); i++) {
        _command[i] = pgm_read_byte_near(RST + i);
      }
    default:
      break;
  }

  do {
    if (timout > max_attempts) return text_response;
    timout++;

    for (byte i = 0; i <= (strlen(_command)); i++) {
      mySerial.write(_command[i]);
      delay(1);
    }
    text_response = receive_command(ans);

  } while (!text_response);


  return text_response;
}

thanks.f

UPDATE: made code shorter as per request.

Can you please post a simple but complete program that sends the data and a short but complete program that receives the data that shows the problem.

Your examples satisfy neither of these criteria.

UKHeliBob:
Can you please post a simple but complete program that sends the data and a short but complete program that receives the data that shows the problem.

I paid attention to different words in the request I think :(.

But these are the functions that receive and send. Will edit to make it smaller.

surepic:
Will edit to make it smaller.

... and complete. That means you can copy / paste it into the Arduino IDE and it will compile without any changes or additions.

code is complete now must work. re-edited.

How 'bout itoa?

void setup()
{
  Serial.begin(9600);
  char buffer[6] = " ";
  int id = 256; 
  itoa(id,buffer,10);
  for(byte q = 0;q < 6;q++)
  {
    Serial.write(buffer[q]);
    Serial.println();
  }
   Serial.println(buffer);  
}
2
5
6



256

Tried itoa yesterday but I guess it was taking more space (cant remember in flash or ram) then the one I wrote.

Why buffer[6] but not 3?