Int to String in a very simple and easy way

I tried everything. But I still need to learn a lot.

inputkeyboard is a global String

I enter a value on a 4x4 membrane keyboard

I decided to define the value as a sum of the day, month and year collected from the object created by the RTC (which does not appear in the code below)

The result is a 4-digit value, in today's case it is 2358

I need keyboard input to be equal to int calculodata so that it works when typing on the keyboard, which will reset ESP32

But I already used string(itoa(calculodata) and NumberToString(calculodata)

It does not work.

What is the simplest and easiest way to do this ?

       int calculodata = now.Day()*now.Month()+now.Year();
       Serial.print("fator data é: ");Serial.println(calculodata);
       
          if (inputteclado == ?) {
            delay(100);
            ESP.restart(); // reseta o esp32
          } 

Start by posting a complete sketch that illustrates your problem

How about

          if (inputkeyboard.toInt() == calculodata ) {

Dear, very grateful.

I got to test several things, just missing this toInt()

It worked out.

If you saved the digits to an int as they were read from the keypad then you would not need to convert the 4 digit value

If you use Strings, you can use all their functions.

Colleagues,

I have a String keypad = 123456789;

And I need to get intermediate numbers, for example, 567 or 345, or 78...etc

What I achieved was getting only final numbers using this function:

keypad.substring(keypad.length() - 2, keypad.length()).toInt();

out = 89

How do I get intermediate numbers using this same toInt() structure ?

Will the numbers that you want to isolate always be in the same place in the string and of the same length ?

Have you got any control over the format of the data that is being sent ?

Yes, they will always be in the same place. These are numbers that I'm going to type on a 4x3 keyboard and I only need to get the 3rd and 4th or 4th. the 5th and the 6th. etc.

My first piece of advice would be to use C style strings rather than Strings

As they are of fixed length and place you can read each number string as it arrives and convert it to an int

If you really want to use Strings then you just need to use substring correctly to pick out the elements of the longer String like this


void setup()
{
  Serial.begin(115200);
  String wholeNumber = "123456789";
  String one = wholeNumber.substring(4, 7); //567
  Serial.println(one);
  int oneInt = one.toInt();
  Serial.println(oneInt * 2); //prove that it is an int
}

void loop()
{
}

Note that the substring end of range index is exclusive (ie not included in the returned string) and that character counting starts at zero, not 1

Thank you very much.

And, by chance, can you help me on this topic ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.