C++ Concat&x27;用整数组成一个字符串

C++ Concat&x27;用整数组成一个字符串,c++,C++,这就是我想做的: int x = 0; char toBuffer; while (twenty_byte_buffer[x] != '\0') // While the string isn't at the end... { cout << int(twenty_byte_buffer[x]); // show me, works fine //need to concat the int values from above into toBuffer as a string

这就是我想做的:

int x = 0;
char toBuffer;
while (twenty_byte_buffer[x] != '\0') // While the string isn't at the end...
{
cout << int(twenty_byte_buffer[x]); // show me, works fine

//need to concat the int values from above into toBuffer as a string
//eg. "-62-8711097109" would have derived from this "©nam"

//this doesn't work:
//strcat ( toBuffer, reinterpret_cast<*????*>(twenty_byte_buffer[x]) );

x++;
} 
intx=0;
炭托布弗;
while(二十字节缓冲区[x]!='\0')//当字符串不在末尾时。。。
{

不能使用
stringstream
。它的工作原理与
cout

stringstream sstr;
while (twenty_byte_buffer[x] != '\0')
{
    sstr << int(twenty_byte_buffer[x]);
    x++;
}

string result = sstr.str();
stringstream-sstr;
而(二十字节缓冲区[x]!='\0')
{
sstr
#包括
std::stringstream sout;
while(二十字节缓冲区[x]!='\0')//当字符串不在末尾时。。。
{

cout最简单的选择是
std::stringstream

#include <sstream>
#include <iostream>
using namespace std;

int main(){
    stringstream numbers;
    for(int i=0; i < 10; ++i){
        numbers << i;
    }
    cout << numbers.str() << endl; // prints "0123456789"
}
#包括
#包括
使用名称空间std;
int main(){
串流数;
对于(int i=0;i<10;++i){

数字正确答案取决于
二十字节缓冲区中的实际内容

如果二十字节缓冲区中的值是代表整数的ASCII字符(包含值“0”到“9”),则需要稍微修改发布的stringstream解决方案,删除
(int)
强制转换:

stringstream sstr;
int x = 0;
while (twenty_byte_buffer[x] != '\0')
{
    // Note no cast to int here...
    sstr << twenty_byte_buffer[x];
    x++;
}

string result = sstr.str();

我的大脑刚刚翻转过来。我不知道这是否是正确的解决方案——请看我在#4996093的答案。(+1,尽管如此,因为我的答案是基于你的答案。)我的目标是匹配(
===
)字符串,这些字符串中显示一些2字节unicode字符,例如
“©nam”
当转换为concat'ed
int
时,将变成
“-62-8711097109”
。当转换成字符串时,我可以快速解析,而无需跳转来处理unicode UTF-16转换。不确定这是否是正确的方法,但它看起来确实是最简单的方法。这里的关键是我正在寻找预定义的封装外形。我可以在字符串中运行,以查找
“-62-8711097109”
。到目前为止,我相信这个答案是最好的答案。@cube:我不能理解其中的大部分。我不确定“@nam”是什么,尽管我知道,
@
==U+00A9。UTF-16中的是00A9,UTF-8中的是C2A9。在十进制数字中,这些值是“169”或“49833”。这些值都不会出现在字符串中“-62-8711097109”。尽管如此,如果字符串以UTF-8编码(如您所说,这意味着某些值是2字节的),则所有字节都不应具有0值,这意味着您可以将
'\0'
作为终止条件。
stringstream sstr;
int x = 0;
while (twenty_byte_buffer[x] != '\0')
{
    // Note no cast to int here...
    sstr << twenty_byte_buffer[x];
    x++;
}

string result = sstr.str();
char toBuffer[100];
int x = 0;
int y = 0;

// I think we still need a different terminating condition...
while (twenty_byte_buffer[x] != '\0') {
    itoa(bytes[x], &toBuffer[y], 10));
    y += strlen(&toBuffer[y]);
}