C++中的多个字符
Multiple chars in C++
如何正确连接DataBuffer
?从PHP到C++并不容易,哈哈,我到底做错了什么?
while(true) {
DWORD TitleID = XamGetCurrentTitleId();
char DataBuffer[] = "Here's the current title we're on : ";
char DataBuffer[] = (DWORD)TitleID;
char DataBuffer[] = "nn";
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
}
在C++中,通常使用std::string
类型。要从多个输入创建一个字符串,特别是如果它们一开始不是全部字符串,我们使用ostringstream
。
所以你可以建立这样的信息:
while(true) {
DWORD TitleID = XamGetCurrentTitleId();
std::ostringstream titleMessageSS;
titleMessageSS << "Here's the current title we're on : "
<< TitleID // already a DWORD, no need for the cast
<< "nn";
std::string titleMessage = titleMessageSS.str(); // get the string from the stream
DWORD dwBytesToWrite = (DWORD)titleMessage.size();
// You don't do anything with this, so can't help you with how to write the string...
}
现在,如果使用WriteToFile
,则需要从字符串中获取一个char
指针。使用titleMessage.c_str()
执行此操作。
或者,您可以使用+
来构建std::string
,再加上std::to_string
来转换TitleID
(这样您就可以用string
来+
)。
尽管PHP可以自动转换为字符串,但C++没有(除了某些情况)。你(通常)必须明确你的类型。幸运的是,C++11提供了std::to_string
,这使得这变得容易多了:
while(true) {
DWORD TitleID = XamGetCurrentTitleId();
std::string DataBuffer = "Here's the current title we're on : "
+ std::to_string(TitleID)
+ "nn";
DWORD dwBytesToWrite = static_cast<DWORD>(DataBuffer.size());
/* ... */
}
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存