Table of Contents
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
// 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
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
// https://pijaeducation.com/data-type-conversion-arduino/ int iVar; float fVar = 1.2; char caVar[10]; String sVar; void setup() { Serial.begin(9600); Serial.println("Defined Variables are -"); Serial.println("int iVar;"); Serial.println("float fVar = 1.2;"); Serial.println("char caVar[10];"); Serial.println("String sVar;"); Serial.println(); /* "FLOAT to OTHER" Data type conversion*/ Serial.println("FLOAT to OTHER Data type conversion"); Serial.println(""); iVar = int(fVar); Serial.print("float 1.2 to int :"); Serial.println(iVar); Serial.println(""); Serial.println("float 1.2 to char Array :"); /* dtostrf(floatVar, minimum_Total_Digits_Including_Decimal_Point, total_Digits_After_Decimal, charArrayVar); floatVar → float charArrayVar[10] → char */ // at least 4 digits including decimal point, and compulsorily 2 digits after the decimal point dtostrf(fVar, 4, 2, caVar); Serial.print("dtostrf(fVar, 4, 2, caVar):"); Serial.println(caVar); // at least 5 digits (otherwise include spaces), and compulsorily 2 digits after the decimal point dtostrf(fVar, 5, 2, caVar); Serial.print("dtostrf(fVar, 5, 2, caVar):"); Serial.println(caVar); // at least 4 digits, and compulsorily 4 digits after the decimal point (otherwise include zeros '0') dtostrf(fVar, 4, 4, caVar); Serial.print("dtostrf(fVar, 4, 4, caVar):"); Serial.println(caVar); Serial.println(""); sVar = String(fVar); Serial.print("float 1.2 to String :"); Serial.println(sVar); Serial.println(); } void loop() { // put your main code here, to run repeatedly: }
OUTPUT
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
// 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
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
// 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
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
// 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
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
// 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() { }
There seems like a lot of information here, but unfortunately you have not provided any explanation to any of the code. You a bit here or there, but data type conversion I believe is in the “beginners” realm. I know a little about this code and I can’t even figure out what some of the examples are trying to tell us.
I think a few more comments within the code would be greatly beneficial.
Thank you.