程序员社区

C++ 类型转换(内置类型,string、char*、const char*等)

目录

类型

转换

全部代码


文章适用于已经学了C++语言,对C++类型有一定了解的朋友。对类型转换进行总结,包括内置类型,string,以及由c语言引入的一些类型。

类型

内置数据类型 字节 范围
char 1 个字节 -128 到 127 或者 0 到 255
unsigned char 1 个字节 0 到 255
signed char 1 个字节 -128 到 127
int 4 个字节 -2147483648 到 2147483647
unsigned int 4 个字节 0 到 4294967295
signed int 4 个字节 -2147483648 到 2147483647
short int 2 个字节 -32768 到 32767
unsigned short int 2 个字节 0 到 65,535
signed short int 2 个字节 -32768 到 32767
long int 8 个字节 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int 8 个字节 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int 8 个字节 0 到 18,446,744,073,709,551,615
float 4 个字节 精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double 8 个字节 双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long double 16 个字节 长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t 2 或 4 个字节 1 个宽字符

字符串

string C++的类

C风格 const char*、char*、char[]

转换

C++类型转换主要分为两种:隐式类型转换、显式类型转换(强制类型转换)。

所谓隐式类型转换,是指不需要用户干预,编译器默认进行的类型转换行为。

四种强制类型转换操作符:static_cast、const_cast、dynamic_cast、reinterpret_cast

还有类的构造方法,类的方法、流等

类型转换及方法
类型转换左转右 方法
char->其他内置类型及const char*、char* 隐式转换
unsigned char、signed char->其他内置类型 隐式转换
unsigned char、signed char->const char*、char* 先转为char,再隐式转换
char、unsigned char、signed char->string 构造方法,push_back、insert等方法
[signed/unsigned] [short/long] int->[signed/unsigned] char +'0'  数值为0~9
[signed/unsigned] [short/long] int->float/[long] double或[signed/unsigned] [short/long] int 隐式转换
float/[long] double -> [signed/unsigned] [short/long] int 显示转换,否则警告(例如,warning C4244: “初始化”: 从“float”转换到“short”,可能丢失数据)
string->const char * 使用string的c_str()函数
string->const char *->char* 先转const char *,再使用const_cast<char*>()转char*
[const] char *->string 隐式转换
const char *->char* 使用const_cast<char*>()转char*
[const] char *->char[] 使用strncpy_s()函数
char* ->const char* 隐式转换

注:[]表示可选,/表示或者

总结

内置类型隐式转换即可,可能丢失数据

涉及指针使用const_cast,涉及string类使用方法push_back、c_str等

建议不要使用C风格的const char *、char[]等,string不香吗???

    string s("nkjsf");
    const char* ch = s.c_str();
    cout << ch << endl;
    s = "efefd";
    cout << ch << endl;

nkjsf

efefd

ch没有变,但是两次输出不一样。各位学完C再学C++的,字符串这里学学string吧,真的很不错。

全部代码

测试了部分,运行单个函数即可。

/*
Project:TypeConvert
Date:    2020/09/06
Author:  Frank Yu
*/
#include<iostream>
#include<string>
#include<typeinfo>
using namespace std;

void display(string type,string method)
{
    cout <<"转为:"<<type<< ",使用方法:" << method << endl;
}

void char2other(char c)
{
    unsigned char uc = c;
    display("unsigned char","隐式转换");
    cout << "转换后:" << uc<<endl;
    signed char sc = c;
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int i = c;
    display("int", "隐式转换");
    cout << "转换后:" << i << endl;
    unsigned int ui = c;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << ui << endl;
    signed int si = c;
    display("signed int", "隐式转换");
    cout << "转换后:" << si << endl;
    unsigned short int usi = c;
    display("unsigned short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    short int s_i = c;
    display("short int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = c;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = c;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = c;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = c;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = c;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = c;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = c;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1,c);
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(c);
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    const char* cc = &c;
    display("const char*", "隐式转换");
    cout << "转换后:" << cc << endl;
    char* c_c = &c;
    display("char*", "隐式转换");
    cout << "转换后:" << c_c << endl;
}

void unsignedchar2other(unsigned char c)
{
    char uc = c;
    display("char", "隐式转换");
    cout << "转换后:" << uc << endl;
    signed char sc = c;
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int i = c;
    display("int", "隐式转换");
    cout << "转换后:" << i << endl;
    unsigned int ui = c;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << ui << endl;
    signed int si = c;
    display("signed int", "隐式转换");
    cout << "转换后:" << si << endl;
    unsigned short int usi = c;
    display("unsigned short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    short int s_i = c;
    display("short int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = c;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = c;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = c;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = c;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = c;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = c;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = c;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, c);
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(c);
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    //先转为了char,因为直接转不行(unsigned char *无法转为const char *)
    char h = c;
    const char* cc = &h;
    display("const char*", "显示转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &h;
    display("char*", "显示转换");
    cout << "转换后:" << *c_c << endl;
}

void signedchar2other(signed char c)
{
    char uc = c;
    display("char", "隐式转换");
    bool b = uc == c;
    cout << b;
    if (uc == c)cout << b;
    cout << "转换后:" << uc << endl;
    unsigned char sc = c;
    display("unsigned char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int i = c;
    display("int", "隐式转换");
    cout << "转换后:" << i << endl;
    unsigned int ui = c;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << ui << endl;
    signed int si = c;
    display("signed int", "隐式转换");
    cout << "转换后:" << si << endl;
    unsigned short int usi = c;
    display("unsigned short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    short int s_i = c;
    display("short int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = c;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = c;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = c;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = c;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = c;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = c;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = c;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, c);
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(c);
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    //先转为了char,因为直接转不行(signed char *无法转为const char *)
    char h = c;
    const char* cc = &h;
    display("const char*", "显示转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &h;
    display("char*", "显示转换");
    cout << "转换后:" << *c_c << endl;
}

void int2other(int i)
{
    char c = i+'0';
    display("int", "+'0'");
    cout << "转换后:" << c << endl;
    unsigned char uc = i+'0';
    display("unsigned char", "隐式转换");
    cout << "转换后:" << uc << endl;
    signed char sc = i + '0';
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    unsigned int ui = i;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << ui << endl;
    signed int si = i;
    display("signed int", "隐式转换");
    cout << "转换后:" << si << endl;
    unsigned short int usi = i;
    display("unsigned short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    short int s_i = i;
    display("short int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = i;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = i;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = i;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = i;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = i;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = i;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = i;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, i+'0');
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(i+'0');
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    const char* cc = &c;
    display("const char*", "隐式转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &c;
    display("char*", "隐式转换");
    cout << "转换后:" << *c_c << endl;

}

void unsignedint2other(unsigned int i)
{
    char c = i + '0';
    display("int", "+'0'");
    cout << "转换后:" << c << endl;
    unsigned char uc = i + '0';
    display("unsigned char", "隐式转换");
    cout << "转换后:" << uc << endl;
    signed char sc = i + '0';
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int ui = i;
    display("int", "隐式转换");
    cout << "转换后:" << ui << endl;
    signed int si = i;
    display("signed int", "隐式转换");
    cout << "转换后:" << si << endl;
    unsigned short int usi = i;
    display("unsigned short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    short int s_i = i;
    display("short int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = i;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = i;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = i;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = i;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = i;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = i;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = i;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, i + '0');
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(i + '0');
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    const char* cc = &c;
    display("const char*", "隐式转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &c;
    display("char*", "隐式转换");
    cout << "转换后:" << *c_c << endl;

}

void signedint2other(signed int i)
{
    char c = i + '0';
    display("int", "+'0'");
    cout << "转换后:" << c << endl;
    unsigned char uc = i + '0';
    display("unsigned char", "隐式转换");
    cout << "转换后:" << uc << endl;
    signed char sc = i + '0';
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int ui = i;
    display("int", "隐式转换");
    cout << "转换后:" << ui << endl;
    unsigned int si = i;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << si << endl;
    unsigned short int usi = i;
    display("unsigned short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    short int s_i = i;
    display("short int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = i;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = i;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = i;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = i;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = i;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = i;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = i;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, i + '0');
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(i + '0');
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    const char* cc = &c;
    display("const char*", "隐式转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &c;
    display("char*", "隐式转换");
    cout << "转换后:" << *c_c << endl;

}

void shortint2other(short int i)
{
    char c = i + '0';
    display("int", "+'0'");
    cout << "转换后:" << c << endl;
    unsigned char uc = i + '0';
    display("unsigned char", "隐式转换");
    cout << "转换后:" << uc << endl;
    signed char sc = i + '0';
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int ui = i;
    display("int", "隐式转换");
    cout << "转换后:" << ui << endl;
    unsigned int si = i;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << si << endl;
    unsigned short int usi = i;
    display("unsigned short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    signed int s_i = i;
    display("signed int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = i;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = i;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = i;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = i;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = i;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = i;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = i;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, i + '0');
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(i + '0');
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    const char* cc = &c;
    display("const char*", "隐式转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &c;
    display("char*", "隐式转换");
    cout << "转换后:" << *c_c << endl;

}

void unsignedshortint2other(unsigned short int i)
{
    char c = i + '0';
    display("int", "+'0'");
    cout << "转换后:" << c << endl;
    unsigned char uc = i + '0';
    display("unsigned char", "隐式转换");
    cout << "转换后:" << uc << endl;
    signed char sc = i + '0';
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int ui = i;
    display("int", "隐式转换");
    cout << "转换后:" << ui << endl;
    unsigned int si = i;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << si << endl;
    short int usi = i;
    display("short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    signed int s_i = i;
    display("signed int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = i;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = i;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = i;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = i;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = i;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = i;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = i;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, i + '0');
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(i + '0');
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    const char* cc = &c;
    display("const char*", "隐式转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &c;
    display("char*", "隐式转换");
    cout << "转换后:" << *c_c << endl;
}

void signedshortint2other(signed short int i)
{
    char c = i + '0';
    display("int", "+'0'");
    cout << "转换后:" << c << endl;
    unsigned char uc = i + '0';
    display("unsigned char", "隐式转换");
    cout << "转换后:" << uc << endl;
    signed char sc = i + '0';
    display("signed char", "隐式转换");
    cout << "转换后:" << sc << endl;
    int ui = i;
    display("int", "隐式转换");
    cout << "转换后:" << ui << endl;
    unsigned int si = i;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << si << endl;
    short int usi = i;
    display("short int", "隐式转换");
    cout << "转换后:" << usi << endl;
    signed int s_i = i;
    display("signed int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = i;
    display("long int", "隐式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = i;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = i;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
    float f = i;
    display("float", "隐式转换");
    cout << "转换后:" << f << endl;
    double d = i;
    display("double", "隐式转换");
    cout << "转换后:" << d << endl;
    long double ld = i;
    display("long double", "隐式转换");
    cout << "转换后:" << ld << endl;
    wchar_t wc = i;
    display("wchar_t", "隐式转换");
    cout << "转换后:" << wc << endl;
    string s(1, i + '0');
    display("string", "构造方法string(size_t n,char c)");
    cout << "转换后:" << s << endl;
    string s1;
    s1.push_back(i + '0');
    display("string", "push_back");
    cout << "转换后:" << s1 << endl;
    const char* cc = &c;
    display("const char*", "隐式转换");
    cout << "转换后:" << *cc << endl;
    char* c_c = &c;
    display("char*", "隐式转换");
    cout << "转换后:" << *c_c << endl;
}

void float2int(float f)
{
    int i = f;
    display("int", "隐式转换");
    cout << "转换后:" << i << endl;
    unsigned int ui = f;
    display("unsigned int", "隐式转换");
    cout << "转换后:" << ui << endl;
    short int si = (short int)f;
    display("short int", "显式转换");
    cout << "转换后:" << si << endl;
    signed int s_i = f;
    display("signed int", "隐式转换");
    cout << "转换后:" << s_i << endl;
    long int li = (long int)f;
    display("long int", "显式转换");
    cout << "转换后:" << li << endl;
    signed long int sli = f;
    display("signed long int", "隐式转换");
    cout << "转换后:" << sli << endl;
    unsigned long int uli = f;
    display("unsigned long int", "隐式转换");
    cout << "转换后:" << uli << endl;
}

void string2other(string s)
{
    const char * cc; 
    cc = s.c_str();
    display("const char *", "使用方法c_str");
    cout << "转换后:" << cc << endl;
    char * c = const_cast<char*>(cc);
    display("char *", "c_str转为const char *,再使用const_cast<char*>()转为char *");
    cout << "转换后:" << c << endl;
}

void constchar2other(const char* cc)
{
    string s = cc;
    display("string", "隐式转换");
    cout << "转换后:" << s << endl;
    char * c = const_cast<char*>(cc);
    display("char *", "const_cast<char*>()");
    cout << "转换后:" << c << endl;
    char ca[10];
    display("char[]", "strncpy_s()");
    strncpy_s(ca, cc, 10);
    cout << "转换后:" << ca << endl;
}

void char_2other(char* c)
{
    string s = c;
    display("string", "隐式转换");
    cout << "转换后:" << s << endl;
    const char * cc = c;
    display("const char *", "隐式转换");
    cout << "转换后:" << cc << endl;
    char ca[10];
    display("char[]", "strncpy_s()");
    strncpy_s(ca, c, 10);
    cout << "转换后:" << ca << endl;
}

int main()
{
    //char2other('n');
    unsignedchar2other('n');
    //signedchar2other('n');
    //signedchar2other('n');
    //int2other(9);
    //unsignedint2other(9);
    //float2int(9.3);
    //string2other("abcd");
    //const char* cc = "abcd";
    //constchar2other("abcd");
    //char_2other("sdfesgrf");

    //问题1  ch会变
    //string s("nkjsf");
    //const char* ch = s.c_str();
    //cout << ch << endl;
    //s = "efefd";
    //cout << ch << endl;
    //问题2  char2other函数输出cc没有烫 unsignedchar2other输出cc有烫
    //char c = 'n';
    //const char *cc = &c;
    //cout << cc << endl;
}
赞(0) 打赏
未经允许不得转载:IDEA激活码 » C++ 类型转换(内置类型,string、char*、const char*等)

一个分享Java & Python知识的社区