unsigned char* 类型数据转换字符串 string

如下方法
//const unsigned short *p = (const unsigned short *)apBulkBuf;
//const WORD *p = (const WORD *)apBulkBuf;
if (*(unsigned short *)apBulkBuf == kSynWordDeviceInfoValue)
//if (*p == kSynWordDeviceInfoValue)
{
	// TODO:
	// 1
	//QString qstrContent; 
	//unsigned char* uszContent = (unsigned char *)qstrContent.toUtf8().data(); 
	QString qstrContent = QString::fromUtf8(QByteArray((char*)apBulkBuf));
	// 2
	std::string str = std::string((const char *)apBulkBuf);
	// 3
	QString str4 = QString::fromLocal8Bit((const char *)apBulkBuf, aNBufSize);
	//QByteArray byteArrayTempInfo;
	//byteArrayTempInfo.toHex();
	// 4
	std::string str2(apBulkBuf, apBulkBuf + aNBufSize);
	// 5
	QString str3 = (char*)apBulkBuf;
	// 6
	char* strTemp = new char[aNBufSize];
	memcpy(strTemp, apBulkBuf, aNBufSize);
	std::string str6 = strTemp;
	// 7
	QString str7 = QString(QLatin1String((char*)apBulkBuf));

	// 写文件
	std::ofstream out("\\temp\\out.txt");
	if (out.is_open())
	{
		out << "1. " << str << "\n";
		out << "2. " << str2 << "\n";
		out << "3. " << str3.toStdString() << "\n";
		out << "4. " << str4.toStdString() << "\n";
		out << "5. " << qstrContent.toStdString() << "\n";
		out << "6. " << str6 << "\n";
		out << "7. " << str6 << "\n";
		out.close();
	}
	std::ofstream fout("\\temp\\a.dat", std::ios::binary);
	fout.write((char*)apBulkBuf, sizeof(char) * (aNBufSize));
	fout.close();



	QLinesSemaphores::m_FreeBytes.release();
	return;
}

你可能感兴趣的:(C++)