Go Down

Topic: String to int (Read 367 times) previous topic - next topic

KikoSmire

Sep 15, 2018, 12:46 pm Last Edit: Sep 15, 2018, 12:49 pm by KikoSmire
How can i convert string s into something like int i?

s = "a==1"

i = a==1

UKHeliBob

Do you want to convert a String (uppercase S, an object created using the String library) or a string (lowercase s, a zero terminated array of chars) ?

Either way I don't understand what you want to do.  More detail and full code please.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GolamMostafa

#2
Sep 15, 2018, 01:53 pm Last Edit: Sep 15, 2018, 03:52 pm by GolamMostafa
How can i convert string s into something like int i?

s = "a==1"

i = a==1
1.  We hope that you know the definition of a string. A string (not String) is an array of similar items in which each item is a printable character of the English Language Alphabet. The style of the declaration/definition of a string containing the 4 characters namely a = = 1 is:

Code: [Select]
char s[] = {'a', '=', '=', '1', '\0'};//'\n'};  //items are shown as charcaters; last item is called null character

or

char s[] =  "a==1";     //the compiler will automatically add null byte at the compile time

or

char s[] = {0x61, 0x3D, 0x3D, 0x31, 0x00}; //items are shown as their actual representation in memory


2. The term integer = int has two meanings/definitions -- in decimal domain, the meaning is limited to the permissible combinations of the digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9; in hexadecimal domain, the meaning is limited to the permissible combinations of the digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A (10), B (11), C (12), D (13), E (14), and F (15).

3.  Now you tell us clearly what you want --
(a)  You want your string "a==1" to be transformed into a==1 which is not an integer?
(b)  What else?

4.  For example:
If you say that your string is char s[] = "1234";, then this string could be transformed to valid integer (1234) using the function atoi().The atoi() function demands that the string must contain items limited to digits 0 to 9.

5.  Please, study the following simple codes and observe the relevance of the above discussion.
Code: [Select]
void setup()
{
  Serial.begin(9600);
  char s1[] = "a==1";
  char s2[] = {'a', '=', '=', '1', '\0'};//'\n'};
  char s3[] = {0x61, 0x3D, 0x3D, 0x31, 0x00};
  char s4[] = "1234";
  char s5[] = "ABCD";
  char s6[] = "ABcD";

  int x1 = atoi(s1);  //function fails, because s1[] contains non-digit character
  Serial.println(x1); //shows: 0

  int x2 = atoi(s2);
  Serial.println(x2); //shows: 0

  int x3 = atoi(s3);
  Serial.println(x3); //shows: 0

  int x4 = atoi(s4); //function passes, because s4[] contains all digit character
  Serial.println(x4); //shows: 1234

  int x5 = atoi(s5);  //function fails, because s5[] contains non-digit character
  Serial.println(x5); //shows: 0

  int x6 = atoi(s6);  //function fails, because s6[] contains non-digit character
  Serial.println(x6); //shows: 0

}

void loop()
{

}


Edit: Edited to include comment of Post#3.

AWOL

Code: [Select]
char s[] = {'a', '=', '=', '1', '\n'};  //items are shown as charcaters; last item is called null character
@GolamMostafa the null character is written '\0', not '\n', which is newline
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

GolamMostafa

Code: [Select]
char s[] = {'a', '=', '=', '1', '\n'};  //items are shown as charcaters; last item is called null character
@GolamMostafa the null character is written '\0', not '\n', which is newline
Thank you for the good catch of Big Hilsha in the Full Monsoon Mousum of Purba Bangha!

boolrules

Are you looking for something like:

eval( "a == 1" );

GolamMostafa

Are you looking for something like:

eval( "a == 1" );
When the OP has problem of declaring a character array in the right way, how can he understand the functioning of the eval() function unless you illustrate it with some examples?

KikoSmire

I have String a and I want to convert it to int b so that b looks like this.

a = "1==1"

b = 1==1

In case b = 1==1 arduino recognizes it as 1(true), but if b = 2==1 arduino recognizes it as 0(false) so I
can put b in if statement.

example code:

int b = 1==1;

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

void loop(){
  if(b){
    Serial.println("true");
    b = 0==1;
  }else{
    Serial.println("false");
    b = 1==1;
  }
  delay(1000);
}

UKHeliBob

Code: [Select]

int b = 1==1;

    b = 0==1;

    b = 1==1;

What do you expect these line of code to do ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

KikoSmire

Code: [Select]

int b = 1==1;

    b = 0==1;

    b = 1==1;

What do you expect these line of code to do ?
It will be saved as true(1)/false(0) for if statement.

boolrules

When the OP has problem of declaring a character array in the right way, how can he understand the functioning of the eval() function unless you illustrate it with some examples?
I guess I"m assuming that he can read and is not a dumbass.

PieterP

You want to save it as a Boolean value, it doesn't make sense to save true/false as an integer.
There are no strings involved.

Code: [Select]
int lhs = 42;
int rhs = 42;
bool equal = lhs == rhs;

if (equal)
  Serial.println("Equal");


Pieter

KikoSmire

#12
Sep 16, 2018, 07:53 pm Last Edit: Sep 16, 2018, 07:57 pm by KikoSmire
You want to save it as a Boolean value, it doesn't make sense to save true/false as an integer.
There are no strings involved.

Code: [Select]
int lhs = 42;
int rhs = 42;
bool equal = lhs == rhs;

if (equal)
  Serial.println("Equal");


Pieter
I understand that but I'm making arduino OS so I'm reading every line from file on SD card(in form of String) and then I have to do command and it definitions(I am reading command from String that I created).

AWOL

#13
Sep 16, 2018, 08:20 pm Last Edit: Sep 16, 2018, 08:22 pm by AWOL
Can I just get this straight/clear - you're making an OS, but you're using the String class?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

PieterP

I'm making arduino OS
Why ...

Trying to create an OS for an Arduino is just wasting your time. It might be interesting for learning purposes, but in that case, I'd recommend a course or a book as guidance. Running an OS on 2 KiB of RAM is pointless, and for the higher-end microcontrollers, you're better off using FreeRTOS.

You could create a simple domain specific language. However, writing a parser is still tricky. For the interpreter, you'll probably need some kind of dynamic type information, expression trees for evaluating expressions, etc.
From a performance point of view, it's much better to just use C/C++. You could even use Python or another high-level language to generate the code for you, if your specific problem benefits from it.
Interpreted languages are fine on high-performance computers with tons of RAM, but aren't a good idea on microcontrollers.
Not only performance and memory usage are problems, you'll also need a clean error-handling solution. What happens if the user made a syntax mistake in the commands on the SD card? The C/C++ compiler will tell you about it if you're using the Arduino IDE. If you use an interpreted language on the Arduino, it'll be a lot harder to debug.

Go Up