c java 数据类型转换,C++ 数据类型转换

1、隐式类型转换(自动转换)

在多种数据类型混合计算的时候,不需要程序员控制类型,系统会自动进行类型转换

转换,隐式转换的规则是:存储长度较短的转换成存储长度较长的,且不丢失数据。

bool - > char - > short int - > int - > unsigned int - > long - > unsigned - > long long - > float - > double - > long double

例如,#include

using namespace std;

int main(){

int x = 10;

char y = 'a';

// 'a' is 97

x = x + y;

float z = x + 1.0;

printf("x = %d, z = %f", x, z);

return 0;

}

赋值转换:如果赋值运算符两侧数据类型不一致,则在赋值时会发生赋值类型转换。包括函数返回值、函数传参等。

例如,#include

using namespace std;

int main(){

int a1 = 3.5;

//此时a的值是3

float f1 = 4;

//此时f的值是4.0

int a, b;

double x = 1.54;

char ch;

a = x;

x = 12;

b = 'a';

ch = 356;

printf("a=%d\nx=%f\nb=%d\nch=\'%c\'\n",a,x,b,ch);

return 0;

}

2、强制类型转换(显式转换)

强制类型转换是通过类型转换运算来实现的。其一般形式为:(类型说明符)(表达式)其功能是把表达式的运算结果强制转换成类型说明符所表示的类型。自动转换是在源类型和目标类型兼容以及目标类型广于源类型时发生一个类型到另一类的转换。

1)C++ 类型显式转换

如 (float)a把a转换为浮点型,(int)(x+y) 把x+y的结果转换为整型。

例如,#include

using namespace std;

int main(){

double x = 1.2;

int sum = (int)x + 1;

printf("sum = %d", sum);

return 0;

}

2)C++类对象的显式转换

C++有四大强制类型转换符:reinterpret_cast, const_cast, static_cast, dynamic_cast。

reinterpret_cast:最普通的强制类型转换符,与用圆括号实现类型转换一样。

const_cast:去掉表达式的const。

static_cast:编译器可以隐式转换的任何类型都可以由static_cast完成。仅当类型之间可隐式转换时(除类层次间的下行转换以外)。static_cast才是合法的。主要是可以提高隐式转换的安全性。另外,C++基本类型(int,char等)的指针之间不能含有隐式转换,必须要用显示转换,所以如果static_cast里有基本类型的指针转换则是错误的。

dynamic_cast:运算符将一个指向基类的指针转换成指向派生类的指针;如果失败,返回空指针。

使用形式为:

cast-name(expression)

例如,#include

using namespace std;

class B{

public:

B():b(1){}

virtual void foo(){};

int b;

};

class D:public B{

public:

D():d(2){}

int d;

};

void func(B *pb){

D *pd1 = static_cast(pb);//语句1

cout<b<

cout<d<

D *pd2 = dynamic_cast(pb);//语句2

cout<b<

cout<d<

}

int main(){

int* ip;

char* p1 = reinterpret_cast(ip);

const char* pc_str;

char* p2 = const_cast(pc_str); //新的表达式pc则删除const属性

B* pb = new B;

func(pb);

return 0;

}

各种类型转换示例如下,#include

#include

#include

using namespace std;

#include

#include

#include

int test_data_type_convert(){

// 1--> int to char[]

int tmp1 = 100;

char ch1[15];

sprintf(ch1, "%d", tmp1);

std::cout << ch1 << std::endl;

// 2--> int to string

int tmp2 = 111;

char ch2[15];

sprintf(ch2, "%d", tmp2);

std::string str2;

str2 = std::string(ch2);

std::cout << str2 << std::endl;

// 3--> int to enum

enum enum3 {

A,

B

};

int tmp3 = 222;

enum3 val3 = static_cast(tmp3);

std::cout << val3 << std::endl;

// 4--> char[] to string

char arr4[] = "this is a sample";

std::string str4;

str4 = std::string(arr4);

std::cout << str4 << std::endl;

std::string str4_;

str4_.assign(arr4);

std::cout << str4_ << std::endl;

// 5--> char to int

char ch5 = '8';

int val5 = ch5 - '0';// val5 is bounded by 0 to 9

std::cout << val5 << std::endl;

// 6--> char[] to int

char arr6[] = "12345";

int tmp6;

sscanf(arr6, "%d", &tmp6);

std::cout << tmp6 << std::endl;

// 7--> char* to int

char* pch7 = "444";

int tmp7;

tmp7 = atoi(pch7);

std::cout << tmp7 << std::endl;

// 8--> char* to float

char* pch8 = "55.5";

float tmp8;

tmp8 = (float)atof(pch8);

std::cout << tmp8 << std::endl;

// 9--> char* to double

char* pch9 = "66.666";

double tmp9;

tmp9 = atof(pch9);

std::cout << tmp9 << std::endl;

// 10--> float to char[]

float tmp10 = 11.11;

char ch10[20];

sprintf(ch10, "%f", tmp10);

std::cout << ch10 << std::endl;

// 11-> int to char*

int tmp11 = 777;

char* pch11;

char ch11[20];

sprintf(ch11, "%d", tmp11);

pch11 = ch11;

std::cout << pch11 << std::endl;

// 12--> double to char[]

double tmp12 = 8.888;

char arr12[20];

sprintf(arr12, "%f", tmp12);

std::cout << arr12 << std::endl;

// 13--> char* to string

char* pch13 = "hello, world";

std::string str13;

str13 = std::string(pch13);

std::cout << str13 << std::endl;

// 14--> string to char[]

std::string str14 = "dog, cat";

char arr14[256];

strncpy(arr14, str14.c_str(), sizeof(arr14));

arr14[sizeof(arr14) - 1] = 0;

std::cout << arr14 << std::endl;

char arr14_[256];

strcpy(arr14_, str14.c_str());

std::cout << arr14_ << std::endl;

// 15--> string to const char*

std::string str15 = "ha ha";

const char* pch15;

pch15 = str15.c_str();

std::cout << pch15 << std::endl;

// 16--> float to int

float ftmp16 = 99.99;

int tmp16;

tmp16 = static_cast(ftmp16);// static_cast(ftmp16 + 0.5)

std::cout << tmp16 << std::endl;

// 17--> vector to float*

std::vector vec;

for (int i = 0; i < 10; i++)

vec.push_back(i * 1.5);

float *p = &vec[0];

for (int i = 0; i < 10; i++)

std::cout << p[i] << std::endl;

// 18--> int[] to vector

int x[5] = {1, 2, 3, 4, 5};

std::vector v(x, x + sizeof(x) / sizeof(x[0]));

for (int i = 0; i < v.size(); i++)

std::cout << v[i] << std::endl;

return 0;

}

int main(){

test_data_type_convert();

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值