How to convert string to int my value not same!

hello guys, please help me...
i want to convert my code. but serial monitor not same with my value..
this is my code. thank you guys

int port = 418171;
String x= " ";
void setup(){
port = (x).toInt();
Serial.print(F("Data port :"));
Serial.println (String(port));
}
void loop(){}

but (x) value is -17365

What is x??

String portString = "418171"; // hum, Port numbers range from 0 to 65535…
void setup(){
  long portNumber = portString.toInt();
  Serial.print(F("Data port :"));
  Serial.println (portNumber);
}

void loop(){}

(Typed from my iPhone, mind typos)

Remember that Port numbers range from 0 to 65535

1 Like

thank you solved !! I change variable port tobe long :grinning:

my problem now how to convert string to char

char  server = "122.122.11.11";
String x ="   "; 
void setup(){
 server = x ;
  Serial.print(F("server:"));
  Serial.println (server);
}

void loop(){}

Do you read what others write to you?
Look carefully at the example above - if you need to convert a String to a something - then the original value must be a String .

Why are you again setting the IP to the char, and the String is empty?

I think that it will better if you try to do it yourself this time.

my code definetely not like that its just sample simple code.
I am sorry my fault ...

char  server = "   ";
String x =" 122.12.12   "; 
void setup(){
 server = x ;
  Serial.print(F("server:"));
  Serial.println (server);
}

void loop(){}

my code like that

What's the purpose of the empty spaces around the value?

Do you seen the documentation for String class in the Arduino reference? Take a look at c_str() method

That’s not good it needs to be

char  server[20] = ""; // whatever size is appropriate 

The char type is just for one character (one byte - so not even utf8 characters if you venture outside common ASCII)

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