Tag Archives: const char to string

DATA TYPE CONVERSION IN ARDUINO

This tutorial covers data type conversion in arduino. Data type conversion or typecasting means converting a value from one data type to other. For example, convert int to float, string to int etc.

Data type covered in this section are int, float, char, char array, string and const char *.

DATA TYPE CONVERSION IN ARDUINO

INT TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from int to other, see below

  • Convert int to float in Arduino
  • Convert int to char in Arduino
  • Convert int to char array in Arduino
  • Convert int to String in Arduino
data type conversion in arduino
Data type conversion: Int to float, char, char array[] and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int a = 65;
float d;
char f;
char h[5];
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int a = 65;");
  Serial.println("float d;");
  Serial.println("char f;");
  Serial.println("char h[5];");
  Serial.println("String j;");
  Serial.println();


  /* "INT to OTHER"
    Data type conversion*/
  Serial.println("INT to OTHER Data type conversion");
  delay(delcat);

  d = a;
  Serial.print("integer 65 to float : ");
  delay(delexp);  
  Serial.println(d);

  delay(delsub);
  f = a;
  Serial.print("int 65 to char : ");
  delay(delexp);  
  Serial.println(f);

  delay(delsub);
  itoa(a, h, 10); // int, char[], base
  Serial.print("int 65 to char array : ");
  delay(delexp);  
  Serial.print(h[0]);
  Serial.print(" , ");
  delay(delexp);  
  Serial.println(h[1]);

  delay(delsub);
  j = String(a);
  Serial.print("int 65 to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}

void loop() {

}

OUTPUT

data type conversion in arduino

FLOAT TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from float to other, see below

  • Convert float to int in Arduino
  • Convert float to String in Arduino
convert float to
Data type conversion: Float to int and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
float c = 1.2;
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("float c = 1.2;");
  Serial.println("String j;");
  Serial.println();


  /* "FLOAT to OTHER"
    Data type conversion*/
  Serial.println("FLOAT to OTHER Data type conversion");
  delay(delcat);

  b = int(c);
  Serial.print("float 1.2 to int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  j = String(c);
  Serial.print("float 1.2 to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();
}

void loop(){
  
}

OUTPUT

data type conversion in arduino

CHAR TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from char to other, see below

  • Convert char to int in Arduino
  • Convert char to float in Arduino
  • Convert char to String in Arduino
char to int
Data type conversion: Char to int, float and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
float d;
char e = 'a';
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("float d;");
  Serial.println("char e = 'a';");
  Serial.println("String j;");
  Serial.println();


  /* "CHAR to OTHER"
    Data type conversion*/
  Serial.println("CHAR to OTHER Data type conversion");
  delay(delcat);

  b = int(e);
  Serial.print("char 'a' to int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  d = float(e);
  Serial.print("char 'a' to float : ");
  delay(delexp);  
  Serial.println(d);

  delay(delsub);
  j = String(e);
  Serial.print("char 'a' to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}
void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino

CHAR ARRAY TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from char array [] or character array to other, see below

  • Convert char array to int in Arduino
  • Convert char array to String in Arduino
char array to String
Data type conversion: Character array to int and String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
char g[] = {'1', '2', '3', 'D', 'E'};
String j;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("char g[] = {'1', '2', '3', 'D', 'E'};");
  Serial.println("String j;");
  Serial.println();


  /* "CHAR ARRAY to OTHER"
    Data type conversion*/
  Serial.println("CHAR ARRAY to OTHER Data type conversion");
  delay(delcat);

  b = atoi(g);
  Serial.print("char array {'1', '2', '3', 'D', 'E'} to int : ");
  delay(delexp);
  Serial.println(b);

  delay(delsub);
  j = String(g);
  Serial.print("char array {'1', '2', '3', 'D', 'E'} to String : ");
  delay(delexp);
  Serial.println(j);
  Serial.println();


}
void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino

STRING TO OTHER DATA TYPE CONVERSION

This Arduino code will convert data types from String to other, see below

  • Convert String to int in Arduino
  • Convert String to char array [] (character array) in Arduino
  • Convert String to const char * in Arduino
string to const char *
Data type conversion: String to int, character array[] and const char pointer
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

int b;
char h[5];
String i = "92AB1";
const char *k;

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("int b;");
  Serial.println("char h[5];");
  Serial.println("String i = \"92AB1\";");
  Serial.println("const char *k;");
  Serial.println();


  /* "String to OTHER"
    Data type conversion*/
  Serial.println("String to OTHER Data type conversion");
  delay(delcat);

  b = i.toInt();
  Serial.print("String 92AB1 to Int : ");
  delay(delexp);  
  Serial.println(b);

  delay(delsub);
  i.toCharArray(h, 10); // buffer (variable in which to store), max length of buffer can be
  Serial.print("String 92AB1 to Char Array : ");
  delay(delexp);  
  Serial.println(h);

  delay(delsub);
  k = i.c_str();
  Serial.print("String 92AB1 to Const char* : ");
  delay(delexp);  
  Serial.println(k);
  Serial.println();


}

void loop() {
  // put your main code here, to run repeatedly:

}

OUTPUT

data type conversion in arduino

CONST CHAR TO STRING CONVERSION

This Arduino code will convert data types from String to other, see below

  • Convert const char * to String in Arduino
const char * to String
Data type conversion: const char * to String
// https://pijaeducation.com/data-type-conversion-arduino/

#define delcat 2000
#define delsub 1500
#define delexp 1000

String j;
const char *l = "HELLO";

void setup() {
  Serial.begin(9600);
  Serial.println("Defined Variables are -");
  Serial.println("String j;");
  Serial.println("const char *l = \"HELLO\";");
  Serial.println();


  /* "Const char to OTHER"
    Data type conversion*/
  Serial.println("Const char to OTHER Data type conversion");
  delay(delcat);

  j = String(l);
  Serial.print("Const Char to String : ");
  delay(delexp);  
  Serial.println(j);
  Serial.println();

}

void loop() {

}

const char to string in arduino

 

 

Total Visits: 6253