Maker Pro
Maker Pro

Help in Arduino Codes

brian22

Jul 19, 2013
40
Joined
Jul 19, 2013
Messages
40
hi sir/ma'am how to convert a char to a string?

i've got this error.

sketch_aug27a:90: error: invalid conversion from 'char' to 'const char*'
sketch_aug27a:90: error: initializing argument 1 of 'String::String(const char*)'
sketch_aug27a:93: error: invalid conversion from 'char' to 'const char*'
sketch_aug27a:93: error: initializing argument 1 of 'unsigned char String::equals(const char*) const'



Code:
char x;
n[20];

     
      if((x, n)>0)   // Number, Number length
      {
      String num=x;
      if(num.equals("12345")){
      Serial.print("Ok");
     }
     else{
     Serial.print("Done");
}
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
String num=x;
Try
Code:
String num=String(x);

I have to admit I'm not familiar with programming an arduino, but this line
Code:
if((x, n)>0)   // Number, Number length
looks suspicious, too. The language reference requires that within the parentheses stand a condition. I do not recognize a condition in (x,n).

Another point:
Code:
String num=x;
      if(num.equals("12345"))
Why do you first convert the number to a string, then compare the string to another number in string format? This is time consuming and unnecessayr. You can directly compare the numbers like:
Code:
if (x==12345)
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
In addition to the above...

Code:
if (12345==x)
is better, because if you omit omit one "=" you get an error rather than a bug.

I also have no idea what

Code:
if((x, n)>0)   // Number, Number length
is meant to do.
 
Top