C++/Qt字符类型转换

字符转换

类型转换是工作中常遇到的问题,本文是根据自己在工作中遇到的一些类型转换问题整理成章的,写的一般,希望对有需要的人能提供一些帮助。

1、float类型转化为QString类型:

float tempSvgValue = svgValue.toFloat()163/4;
QString bufSvgValue = QString("%1").arg(tempSvgValue);

2、QString类型转化为float类型:tofloat()

3、string类型转化为QString类型:

string strCheck;
QString tempStrCheck = QString::fromStdString(strCheck);

4、string型转化为char类型:c_str()

string s=“1234”;
const char *c = s.c_str();
注意,c_str() 返回值为const char *类型的!

5、int类型转化为QString类型:

int num=55;
QString str=QString::number(num,10);//str = 55;

6、QByteArray 转换为 char *

char *ch;//不要定义成ch[n];
QByteArray byte;
ch = byte.data();

7、char * 转换为 QByteArray

char *ch;
QByteArray byte;
byte = QByteArray(ch);

8、QString 转换为 QByteArray

QByteArray byte;
QString string;
byte = string.toAscii();

9、QByteArray 转换为 QString

QByteArray byte;
QString string;
string = QString(byte);

10、long 型转化为QString型

unsigned long int a=1;
QString str;
str=QString::number(a);

11、qint64转换成QString

qint64 code;
QString devcode = QString().setNum(code);

12、TCHAR* 转化为char*型

OPENFILENAME ofn;
TCHAR szFile[MAX_PATH];
string path = “”;
if (GetOpenFileName(&ofn))
{
int iLength ;
char* tempPath = new char[256];
//获取字节长度
iLength = WideCharToMultiByte(CP_ACP, 0, szFile, -1, NULL, 0, NULL, NULL);
//将tchar值赋给_char
WideCharToMultiByte(CP_ACP, 0, szFile, -1, tempPath, iLength, NULL, NULL);
path = tempPath;
}

13、string转换成INT64

string id;
INT64 code ;
code = _atoi64(id.c_str());

14、INT64转化成string

std::string CTVMSKMediator::INT64ToString( INT64 id )
{
string strcode;
char buffer[30];
sprintf(buffer,"%018I64d",id);
strcode=buffer;
return strcode;
}

15、char*转化为string直接赋值即可

char* buf;
string str;
str = buf;

16、string 转化为int

1)string str = “12345”;
int num = 0;
num = std::atoi(str);

17、long转化为string

long EnrollNumber = 1234;
stringstream stream;
string EnrollNumberstr;
stream << EnrollNumber;
stream >> EnrollNumberstr;

18、QString转化为TCHAR

TCHAR* QStringToTchar(QString servName)
{
QTextCodec codec = QTextCodec::codecForName(“GBK”);
QTextCodec::setCodecForLocale(codec);
QByteArray ba = servName.toLocal8Bit();
const char
serNameChar = ba.data();
int iLen = strlen(serNameChar);
TCHAR* serTcharName = new TCHAR[iLen+1];
MultiByteToWideChar(CP_ACP, 0, serNameChar, -1, serTcharName, 100);
return serTcharName;
}

19、WCHAR转化为const char

char szExeFileChar[256];
sprintf(szExeFileChar,"%ws",pe.szExeFile);

20、string类型转化为LPTSTR、LPWSTR

//LPTSTR
string proName;
int l=MultiByteToWideChar(CP_ACP,0,proName.c_str() ,-1,NULL,0);
LPTSTR exeProName=new TCHAR[l];
MultiByteToWideChar(CP_ACP,0,proName.c_str() ,-1,exeProName,l);

// LPWSTR
string appPath = “”;
appPath = QApplication::applicationDirPath().toStdString() + “/pictureFile/testShow.jpg”;
int len = MultiByteToWideChar(CP_ACP, 0, appPath.c_str(), -1, NULL, 0);
LPWSTR filePath = new TCHAR[len];
MultiByteToWideChar(CP_ACP, 0, appPath.c_str(), -1, filePath, len);

21、LPCTSTR name转化为string

string strname = string((char)name);

22、string转化为LPCSTR

string tempPath = “hhhhhhhhh”;
LPCSTR path = tempPath.c_str();

posted @ 2017-11-19 14:34  ISmileLi  阅读(421)  评论(0编辑  收藏  举报