ในบทนี้ คุณจะได้เรียนรู้และเข้าใจเกี่ยวกับการแปลงข้อมูลในภาษา C++ สำหรับแปลงข้อมูลอีกประเภทหนึ่งไปเป็นอีกประเภท ซึ่งการแปลงข้อมูลจะมีอยู่สองแบบใหญ่ๆ คือ Implicit type conversion และ Explicit type conversion และเราจะพูดถึงการแปลงข้อมูลกับข้อมูลประเภท String ที่ใช้ฟังก์ชันจากไลบรารี่ในภาษา C++
ในตัวอย่างนี้ เรามี 3 คำสั่ง และแต่ละคำสั่งได้ทีการแปลงข้อมูลแบบ Implicit conversion เราได้สร้างตัวแปร a และ b ในตัวอย่างเราจะได้ a เป็น 10 เพราะว่าตัวแปร a นั้นเป็น Integer ถึงแม้ว่าเราจะได้กำหนดค่า 10.5 ให้มัน แต่คอมไพเลอร์จะทำการแปลงข้อมูลอัตโนมัติ นี้เรียกว่า Implicit conversion
Expression int c = a / b; และตัวแปร c จะมีค่าเป็น 2 เพราะเกิดจาการทำงานของคำสั่ง a / b เนื่องจากตัวแปรทั้งสองทั้ง a และ b มีประเภทเป็น int ถึงแม้ว่าเราจะใช้คำสั่ง short ในการประกาศตัวแปร b แต่มันมีขนาดเล็กกว่า int และทั้งสองประเภทเก็บข้อมูลประเภทเดียวกัน
int a =5;int b =2;float c = a / b;// 2float d = a /(float) b;// 2.5
สิ่งหนึ่งที่เกิดขึ้นบ่อยในการเขียนโปรแกรมสำหรับผู้เริ่มต้นคือ การหารตัวเลขจำนวนเต็มด้วยจำนวนเต็ม (int / int) นั้นจะได้ผลลัพธ์เป็น Integer เสมอ ถ้าหากคุณต้องการผลลัพธ์ที่เป็นทศนิยม อย่างน้อยหนึ่งค่าใน Expression ต้องเป็นข้อมูลประเภท Float เหมือนในตัวอย่างในตัวแปร c และ d
Type casting (Explicit type conversion)
ในภาษา C++ เราสามารถแปลงข้อมูลโดยการใช้วิธีที่เรียกว่า Type-casting หรือ Explicit type conversion มันมีสองวิธีในการทำคือแบบ functional และ c-like เราสามารถใช้ได้ทั้งแบบ functional และ c-like ข้อแตกต่างของรูปแบบการใช้งานของมันเป็นดังนี้:
double n =10.3;float m =5.5;int x =int(n);// functionalint y =(int) n;// c-like
อย่างไรก็ตามการแปลงข้อมูลด้วยวิธี Explicit type conversion นี้สามารถใช้ได้เพียงกับ Primitive data type ที่สามารถแสดงในรูปแบบของตัวเลขได้เท่านั้น เช่น boolcharintlong``float และ double เป็นต้น
#include<iostream>#include<cmath>#include<string>usingnamespace std;
string int_to_bin(int);intbin_to_int(string);intmain(){
cout <<"Binary / Decimal table"<< endl;
cout <<"Number\tint_to_bin\tbin_to_int\tChar"<< endl;for(int i =0; i <=127; i++){
string bin =int_to_bin(i);int dec =bin_to_int(bin);
cout << i <<"\t"<< bin <<"\t"<< dec
<<"\t"<<(char)i << endl;}return0;}
string int_to_bin(int n){
string bin ="";while(n >0){
bin =(n %2==0?"0":"1")+ bin;
n /=2;}return bin;}intbin_to_int(string s){int len = s.length();int dec =0;for(int i =0; i < len; i++){
dec +=((int)s[i]-'0')*pow(2, len - i -1);}return dec;}
In this tutorial, we will learn about the basics of C++ type conversion with the help of examples.
C++ allows us to convert data of one type to that of another. This is known as type conversion.
There are two types of type conversion in C++.
Implicit Conversion
Explicit Conversion (also known as Type Casting)
Implicit Type Conversion
The type conversion that is done automatically done by the compiler is known as implicit type conversion. This type of conversion is also known as automatic conversion.
Let us look at two examples of implicit type conversion.
Example 1: Conversion From int to double
// Working of implicit type-conversion#include<iostream>usingnamespacestd;
intmain(){
// assigning an int value to num_intint num_int = 9;
// declaring a double type variabledouble num_double;
// implicit conversion// assigning int value to a double variable
num_double = num_int;
cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
return0;
}
Output
num_int = 9
num_double = 9
In the program, we have assigned an int data to a double variable.
num_double = num_int;
Here, the int value is automatically converted to double by the compiler before it is assigned to the num_double variable. This is an example of implicit type conversion.
Example 2: Automatic Conversion from double to int
//Working of Implicit type-conversion#include<iostream>usingnamespacestd;
intmain(){
int num_int;
double num_double = 9.99;
// implicit conversion// assigning a double value to an int variable
num_int = num_double;
cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
return0;
}
Output
num_int = 9
num_double = 9.99
In the program, we have assigned a double data to an int variable.
num_double = num_int;
Here, the double value is automatically converted to int by the compiler before it is assigned to the num_int variable. This is also an example of implicit type conversion.
Note: Since int cannot have a decimal part, the digits after the decimal point is truncated in the above example.
Data Loss During Conversion (Narrowing Conversion)
As we have seen from the above example, conversion from one data type to another is prone to data loss. This happens when data of a larger type is converted to data of a smaller type.
Possible Data Loss During Type Conversion
C++ Explicit Conversion
When the user manually changes data from one type to another, this is known as explicit conversion. This type of conversion is also known as type casting.
There are three major ways in which we can use explicit conversion in C++. They are:
C-style type casting (also known as cast notation)
Function notation (also known as old C++ style type casting)
Type conversion operators
C-style Type Casting
As the name suggests, this type of casting is favored by the C programming language. It is also known as cast notation.
The syntax for this style is:
(data_type)expression;
For example,
// initializing int variableint num_int = 26;
// declaring double variabledouble num_double;
// converting from int to double
num_double = (double)num_int;
Function-style Casting
We can also use the function like notation to cast data from one type to another.
The syntax for this style is:
data_type(expression);
For example,
// initializing int variableint num_int = 26;
// declaring double variabledouble num_double;
// converting from int to double
num_double = double(num_int);
We used both the C style type conversion and the function-style casting for type conversion and displayed the results. Since they perform the same task, both give us the same output.
Type Conversion Operators
Besides these two type castings, C++ also has four operators for type conversion. They are known as type conversion operators. They are:
static_cast
dynamic_cast
const_cast
reinterpret_cast
We will learn about these casts in later tutorials.