Go Down

### Topic: convert 2 string to a single number (Read 92 times)previous topic - next topic

#### 0Avendano

##### Mar 30, 2019, 02:09 am
what happens is that I have a code of a calculator that at the end you get two numbers, but these two numbers are string so I need if for example number1 = 1 and number2 = 4 I want to get an integer equal to 14 but I do not know how to do it, thanks.

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);

// start the code needed for keypad
const byte rows = 4;
const byte columns = 4;
byte pinsfilas [rows] = {47, 49, 51, 53}; // connect to the row pinouts of the keypad
byte pinscolumns [columns] = {39, 41, 43, 45}; // connect to the column pinouts of the keypad
boolean currentValue = false;
boolean next = false;
final boolean = false;
String number1, number2, number3;
int Total calculation;
int movement;
char operation;
float r1, r2, r3, r4;
decimal float;
int counter = 0;

char keys [rows] [columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'E', '0', 'F', 'D'}
};

// end the necessary keypad code

char key;
void setup () {

Wire.begin ();
lcd.begin (16, 2);
lcd.backlight ();
lcd.setCursor (0,0);
lcd.print ("WELCOME");

}

void loop () {
key = keyboard.getKey ();
int length of the number;

// I check that a certain key was pressed.

if (key! = NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key = = '6' || key == '7' || key == '8' || key == '9' || key == '0'))
{

// Initialization of varaibles

if (counter == 1) {
lcd.clear ();
currentValue = false;
final = false;
number1 = "";
number2 = "";
Total calculation = 0;
operation = '';
lcd.setCursor (0,1);
lcd.print (">");
counter = 0;
}

if (currentValue = true) {

number1 = number1 + key;
lengthNumber = number1.length ();
movement = lengthNumber;
lcd.setCursor (1,1);
lcd.print (number1);
} else {
number2 = number2 + key;
lengthNumber = number2.length ();
lcd.setCursor (movement + 2, 0);
lcd.print (number2);
final = true;
}
if (key == 'E')
{
lcd.clear ();
delay (50);
}
}

if (key == 'A')
{
lcd.clear ();
lcd.setCursor (0,0);
lcd.print ("Indicate cycles:");

}

if (key == 'E')
{
lcd.clear ();
lcd.setCursor (0,0);
lcd.print ("Indicate cycles:");
number2 = "";
number1 = "";

}

if (key == 'B')
{
lcd.setCursor (0,0);
lcd.print (number2);
lcd.print (number1);

}

}

#### 0Avendano

#1
##### Mar 30, 2019, 02:34 am
the answer is easy, only use .intTo ()
e = number1.toInt () + number2.toInt ();
so you get if for example you have your string number1 = 1
and string number 2 = 4
and you want to get 14 and not the sum of 1 + 4 just do this
e = number1.toInt () + number2.toInt ();
and will give you 14

#### GolamMostafa

#2
##### Mar 30, 2019, 05:11 amLast Edit: Mar 30, 2019, 06:57 am by GolamMostafa
@OP

1.  Are you entering the numbers (digit 1 and digit 4) from a Keypad connected with the UNO?

2.  Do the digits appear on the LCD side by side like?:
14

3.  You think that the digits are actually being saved into two variables as character data. That is:
Code: [Select]
`char x1 = 0x31;   //for 1char x2 = 0x34;   //for 4`

4.  What do you want to do now? You want something like this: the contents of x1 and x2 are to be merged together to form 14 (00010100) .

5.  The solution is:
Code: [Select]
`byte x = x1;x = x - 0x30;   // x = 00000001   Why is 0x30 being subtracted? Just manipulation! x = x<<4;       // x = 00010000//----------------------------------byte y = x2;y = y- 0x30;   // y = 00000100//---------------------------------byte z = x | y;  // z = 00010100//---------------------------------Serial.println(z, BIN);     //shows: 10100 ---> 00010100 (the leading 0s are not printed)Serial.println(z, HEX);    //shows: 14Serial.println(z, DEC);   //shows: 20`

6.  The alternative solution:
If you think that the entered digits are saved in character type array like this, then the solution is:
Code: [Select]
`char myString[3] = {0x31, 0x34, 0x00};  //ASCII code of 1 4 NULL-byteint z = atoi(myString);Serial.println(z, BIN);     //shows: 1110 --> 00001110Serial.println(z, HEX);    //shows: ESerial.println(z, DEC);   //shows: 14`

7.  Compare the codes and results of Step-5 and Step-6 and then post your comments.

Go Up