How to convert int to string on Arduino?

前端 未结 9 1359
-上瘾入骨i
-上瘾入骨i 2021-01-31 06:32

How do I convert an int, n, to a string so that when I send it over the serial, it is sent as a string?

This is what I have so far:

int ledP         


        
9条回答
  •  时光取名叫无心
    2021-01-31 07:30

    You just need to wrap it around a String object like this:

    String numberString = String(n);
    

    You can also do:

    String stringOne = "Hello String";                     // using a constant String
    String stringOne =  String('a');                       // converting a constant char into a String
    String stringTwo =  String("This is a string");        // converting a constant string into a String object
    String stringOne =  String(stringTwo + " with more");  // concatenating two strings
    String stringOne =  String(13);                        // using a constant integer
    String stringOne =  String(analogRead(0), DEC);        // using an int and a base
    String stringOne =  String(45, HEX);                   // using an int and a base (hexadecimal)
    String stringOne =  String(255, BIN);                  // using an int and a base (binary)
    String stringOne =  String(millis(), DEC);             // using a long and a base
    

提交回复
热议问题