摘要
在平时C++编写过程中,经常用到string类型与char类型,在Qt中显示数据等经常用到QString类型,因此在编写过程中经常需要格式转换,在这里对常用的字符,字符串类型转换做一个总结。
定义基本类型及变量
先定义好常用的数据类型与变量
int number = 100;
QByteArray byteLongArray;
QByteArray byteShortArray("01 02"); //QByteArray初始化
unsigned char uchValue = 50;
unsigned char uchBits = '100';
char chLongArray[100] = {0};
char chValueArray[5] = {12,12,34,45,55};
char chBitsArray[5] = {'12','12','34','45','55'};
QString qstringLong = "02 03 04 01";
QString astringShort = "30";
std::string stringLong = "01 03 04 05";
std::string stringShort = "50";
转换过程
1.string转char*,这样只能转化为const类型
const char *pch = stringLong.c_str();
2.string转char[ ]
//这里要按照自己的格式要求,修改输出格式
for(int i =0;i<stringLong.length();i++)
{
chLongArray[i] = stringLong[i];
}
3.string转int
number = atoi(stringShort.c_str());
4.QString转int
number = astringShort.toInt();
5.QString转unsigned char
bool ok;
uchValue = qstringShort.toInt(&ok,10);
6.QString转char*
//下面步骤不能一步完成
QString qStr = "192.168.1.1";
QByteArray byteArray = qStr.toUtf8();
char *ip = byteArray.data();
7.QString转QByteArray
byteLongArray = qstringLong.toLatin1();
8.int转string
//这里是将数值100转成字符串"100";
stringShort = to_string(number);
//若是数值0x100,数值是16进制,照原样转换成字符串"100",而不是先把0x100变成十进制再转换,用下面的方法;
sprintf_s(stringShort,sizeof(stringShort),"%x",number);
9.char*,char[ ]转string
//这样直接转换,如果char里是数值型,会先转换成ASCII字符,再转成string
stingLong = chValueArray;
//若要把数值型char直接转成数值型字符串显示,先把每个数值转换成字符
//string的合并用的是+,若是想中间空一格,比如22,33,44变成"22 33 44"
//自定义函数,第二个参数是char数组元素个数
std::string charArray2String(char *srcArray,const int charArrayNum)
{
std::string strTemp = "";
std::string temp1="";//数值中间是空格
for(int i = 0;i<charArrayNum;i++)
{
temp1 = to_string(srcArray[i])+" ";
strTemp += temp1;
}
return strTemp;
}
stringLong = charArray2String(chValueArray,3);
//若要把十六进制数值型char直接转成数值型字符串显示,先把每个数值转换成字符
//string的合并用的是+,若是想中间空一格,比如0x22,0x33,0x44变成"22 33 44"
//自定义函数,第二个参数是char数组元素个数
//修改windows底层提供的to_string函数,更改成to_stringHex
inline string to_stringHex(int _Val)
{
char _Buf[2*_MAX_INT_DIG];
_CSTD _TOSTRING(_Buf,"%x",_Val);
return(string(_Buf));
}
std::string charHexArray2String(char *srcArray,const int charArrayNum)
{
std::string strTemp = "";
std::string temp1="";//数值中间是空格
for(int i = 0;i<charArrayNum;i++)
{
temp1 = to_stringHex(srcArray[i])+" ";
strTemp += temp1;
}
return strTemp;
}
char chValueArray[3] = {0x22,0x33,0x44};
stringLong = charHexArray2String(chValueArray,3);
10.QString转string
stringLong = qstingLong.toStdString();
11.string转QString
qstringShort = QString::fromStdString(stringShort);
10.QByteArray转QString
qstringLong = QString(byteShortArray);
12.char*,char[ ]转QString
qstringLong = QString(QLatin1String(chBitsArray));
13.unsigned char转QString
//100是数值,会先将100转成16进制,再转化为字符串"64";
qstringShort = QString::number(uchValue,16);
//100是数值,会先将100转成10进制,再转化为字符串"100";
qstringShort = QString::number(uchValue,10);
//100是单个字符'100',会将'100'按照ASCII值转成字符串"48";
qstringShort = QString::number(uchBits,10);
14.int转QString
//数值100,按照十进制不变,转换成了字符串"100";
qstringShort = QString::number(number);
15.QString转const char *
QString str = "nihao";
const char *chStr = str.toStdString().c_str();