string、const char*、 char* 、char[]四者类型经常会需要转化。

一:转化总结形式如下:

使用时,要对源格式和目标格式进行初始化。源格式赋值为具体的内容,目标格式赋值为空。

string、const char*、 char* 、char[]相互转换(全)

二、总结方法:

1、        变成string,直接赋值。

2、        char[]变成别的,直接赋值。

3、        char*变constchar*容易,const char*变char*麻烦。<const_cast><char*>(constchar*);

4、        string变char*要通过const char*中转。

5、        变成char[]。string逐个赋值,char* const char* strncpy_s()。

三、代码实现(方便初学者,贴的很详细。了解了以上一二点,可跳过代码。)

1、string转为其他类型

①、string转const char*

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     std::string str = "HelloWorld!";     //初始化string类型,并具体赋值

  12.  
  13.     const char* constc = nullptr;         //初始化const char*类型,并赋值为空

  14.  
  15.     constc= str.c_str();                 //string类型转const char*类型

  16.  
  17.     printf_s("%s\n", str.c_str());        //打印string类型数据 .c_str()

  18.  
  19.     printf_s("%s\n", constc);             //打印const char*类型数据

  20.  
  21.     return 0;

  22.  
  23. }


②、string转char*

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     std::string str = "HelloWorld!";     //初始化string类型,并具体赋值

  12.  
  13.     char* c = nullptr;                    //初始化char*类型,并赋值为空

  14.  
  15.     const char* constc = nullptr;         //初始化const char*类型,并赋值为空

  16.  
  17.     constc= str.c_str();                 //string类型转const char*类型

  18.  
  19.     c= const_cast<char*>(constc);        //const char*类型转char*类型

  20.  
  21.     printf_s("%s\n", str.c_str());        //打印string类型数据 .c_str()

  22.  
  23.     printf_s("%s\n",c);                  //打印char*类型数据

  24.  
  25.     return 0;

  26.  
  27. }

③、string转char[]

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     std::string str = "HelloWorld!";      //初始化string类型,并具体赋值

  12.  
  13.     char arrc[20] = {0};                   //初始化char[]类型,并赋值为空

  14.  
  15.     for (int i = 0; i < str.length(); i++) //string类型转char[]类型

  16.  
  17.     {

  18.  
  19.         arrc[i]=str[i];

  20.  
  21.     }

  22.  
  23.     printf_s("%s\n", str.c_str());         //打印string类型数据 .c_str()

  24.  
  25.     printf_s("%s\n", arrc);                //打印char[]类型数据

  26.  
  27.     return 0;

  28.  
  29. }

2、const char*转为其他类型

①const char*转string

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     const char* constc = "Hello World!";     //初始化const char* 类型,并具体赋值

  12.  
  13.     std::string str;                        //初始化string类型

  14.  
  15.     str= constc;                            //const char*类型转string类型

  16.  
  17.     printf_s("%s\n", constc);                //打印const char* 类型数据

  18.  
  19.     printf_s("%s\n", str.c_str());           //打印string类型数据

  20.  
  21.     return 0;

  22.  
  23. }

②const char*转char*

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     const char* constc = "Hello World!";     //初始化const char* 类型,并具体赋值

  12.  
  13.     char* c = nullptr;                       //初始化char*类型

  14.  
  15.     c= const_cast<char*>(constc);           //const char*类型转char*类型

  16.  
  17.     printf_s("%s\n", constc);                //打印const char* 类型数据

  18.  
  19.     printf_s("%s\n", c);                     //打印char*类型数据

  20.  
  21.     return 0;

  22.  
  23. }

③const char*转char[]

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.  
  12.  
  13.     const char* constc = "Hello World!";     //初始化const char* 类型,并具体赋值

  14.  
  15.     char arrc[20] = { 0 };                   //初始化char[]类型,并赋值为空

  16.  
  17.     strncpy_s(arrc,constc,20);              //const char*类型转char[]类型

  18.  
  19.     printf_s("%s\n", constc);                //打印const char* 类型数据

  20.  
  21.     printf_s("%s\n", arrc);                  //打印char[]类型数据

  22.  
  23.     return 0;

  24.  
  25. }

3、char*转为其他类型

①char*转string

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     char* c = "HelloWorld!";           //初始化char* 类型,并具体赋值

  12.  
  13.     std::string str;                   //初始化string类型

  14.  
  15.     str= c;                            //char*类型转string类型

  16.  
  17.     printf_s("%s\n", c);                //打印char* 类型数据

  18.  
  19.     printf_s("%s\n", str.c_str());      //打印string类型数据

  20.  
  21.     return 0;

  22.  
  23. }

②char*转const char*

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.  
  12.  
  13.     char* c = "HelloWorld!";         //初始化char* 类型,并具体赋值

  14.  
  15.     const char* constc = nullptr;     //初始化const char* 类型,并具体赋值

  16.  
  17.     constc= c;                       //char*类型转const char* 类型

  18.  
  19.     printf_s("%s\n", c);              //打印char* 类型数据

  20.  
  21.     printf_s("%s\n", constc);         //打印const char* 类型数据

  22.  
  23.     return 0;

  24.  
  25. }

③char*转char[]

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     char* c = "HelloWorld!";         //初始化char* 类型,并具体赋值

  12.  
  13.     char arrc[20] = { 0 };           //初始化char[] 类型,并具体赋值

  14.  
  15.     strncpy_s(arrc,c,20);             //char*类型转char[] 类型

  16.  
  17.     printf_s("%s\n", c);              //打印char* 类型数据

  18.  
  19.     printf_s("%s\n", arrc);           //打印char[]类型数据

  20.  
  21.     return 0;

  22.  
  23. }


4、char[]转为其他类型

 
  1. #include "stdafx.h"

  2.  
  3. #include <iostream>

  4.  
  5.  
  6.  
  7. int _tmain(intargc, _TCHAR* argv[])

  8.  
  9. {

  10.  
  11.     char arrc[20] = "HelloWorld!";            //初始化char[] 类型,并具体赋值

  12.  
  13.     std::string str;                          //初始化string

  14.  
  15.     const char* constc = nullptr;              //初始化const char*

  16.  
  17.     char*c = nullptr;                          //初始化char*

  18.  
  19.  
  20.  
  21.     str= arrc;                                //char[]类型转string类型

  22.  
  23.     constc= arrc;                             //char[]类型转const char* 类型

  24.  
  25.     c= arrc;                                  //char[]类型转char*类型

  26.  
  27.  
  28.  
  29.     printf_s("%s\n", arrc);                    //打印char[]类型数据

  30.  
  31.     printf_s("%s\n", str.c_str());             //打印string类型数据

  32.  
  33.     printf_s("%s\n", constc);                  //打印const char* 类型数据

  34.  
  35.     printf_s("%s\n", c);                       //打印char*类型数据

  36.  
  37.     return 0;

  38.  
  39. }



转载:https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 

相关文章: