converting int value to const char

Hello everyone.
I have a 4-digit 7 segment LED display and I would like to use a push button to change the number shown on the display.

I start with a very simple push button code, I can change an int value by pushing the button:

int buttonPressCount;
const int  buttonPin = 5;
int buttonPushCounter = 1000;
int buttonState = 0;
int lastButtonState = 0;


void setup()  {
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
    }
  }
  lastButtonState = buttonState;
}

I have another code for the LED display. I can directly input the number I want to show.

#include <string.h>

int DIO = 2;
int RCLK = 3;
int SCLK = 4;

unsigned char CHAR_MAP[] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x8C, 0xBF, 0xC6, 0xA1, 0x86, 0xFF, 0xbf };
char CHAR_INDEX_MAP[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '-' };

void ledout(unsigned char chr)
{
  unsigned char i;
  for (i = 8; i >= 1; i--)
  {
    if (chr & 0x80) digitalWrite(DIO, 1); else digitalWrite(DIO, 0);
    chr <<= 1;
    digitalWrite(SCLK, 0);
    digitalWrite(SCLK, 1);
  }
}

void ledshow(const char* str)
{
  int len = strlen(str);
  int hasDot = 0;
  for (int i = len - 1, m = 0; i >= 0; i--) {
    char chr = str[i];
    if (chr == '.') {
      hasDot = 1;
      continue;
    }
    int n = 0;
    for (; n < 17; n++) {
      if (chr == CHAR_INDEX_MAP[n])
        break;
    }
    if (n != 17) {
      unsigned char chr1 = CHAR_MAP[n];
      if (hasDot)
        chr1 &= 0x7f;
      ledout(chr1);
      ledout((int)ceil(pow(2, m)));
      digitalWrite(RCLK, 0);
      digitalWrite(RCLK, 1);
    }

    m++;
    hasDot = 0;
  }
}

void setup()
{
  pinMode(DIO, OUTPUT);
  pinMode(RCLK, OUTPUT);
  pinMode(SCLK, OUTPUT);
}

void loop()
{
  ledshow("1000");
}

But then there is the problem, I cannot convert the int value (for changing the value) into const char (for displaying the value) and I don’t know how could I put the two code together.
I am totally new to coding, can anyone solve this for me?

Hello and welcome,

Use function itoa

int num = 1000;
char str[8];
itoa( num, str, 10 );
ledout( str );
...

guix: Use function ...

Thanks guix. I look at the link and I understand what could this function do for me. But I am now confused about how should I connect the two codes or what changes should I make (like where should I put the four line you gave me). Should I put them inside int main(){} like the example does? Sorry I ask dump question, but I really need help.

kisa:
But I am now confused about how should I connect the two codes or what changes should I make (like where should I put the four line you gave me).

You won’t need the four lines he gave you. Three of those lines are just there to illustrate how the itoa function works.

Use the itoa function at the point in your code where you want to convert a integer number into a string of ascii characters. It really can’t be more simple than that. Follow through your code line by line thinking about what each line does. When you get to the point where you need to make that conversion, then that’s the place where you’re going to use itoa.

Thanks Delta_G. I think I know what to put in the place of the int value and int base, but I don't know what should I do with the char * str. I actually don't quite understand what is char * str. So this is what I have now:

void loop()
{
   buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
    }
  }
  lastButtonState = buttonState;
  ledshow(itoa(buttonPushCounter,???,10));
}

itoa doesn't return the string, it returns a pointer to the string. So what you've got there won't work.

You need to create a place to put this string. So you create a buffer, like guix did. So I guess you do need one more of his lines. You create a char array big enough to hold your number plus one more character for the terminating null. Then you pass this buffer as an argument to itoa and it will take your number, turn it into ascii, and put it in that buffer for you. Now you can use that buffer like a normal string.

ledshow(itoa(buttonPushCounter,???,10));

Should be like:

char buf[8];
itoa(buttonPushCounter, buf, 10);
ledshow(buf);

Thanks. My problem is solved now and the whole thing works!