简体   繁体   English

在C ++中将整数填充/嵌入到字符串中

[英]cramming/embedding integers into strings in C++

i have some data that has to match a certain packet protocol. 我有一些必须匹配某个数据包协议的数据。 i'm getting said data as a std::string. 我将数据称为std :: string。

i need to take some integer values and put them in the string in certain locations. 我需要取一些整数值并将它们放在某些位置的字符串中。 i don't want to convert the integer to string values; 我不想将整数转换为字符串值; i want to copy the actual byte values in. 我想复制实际的字节值。

so for example, if my string is "abcdefg", and i have a 2 byte short with the value of 25 that i want to put into the 1st position, i don't want "a25defg". 所以例如,如果我的字符串是“abcdefg”,并且我有一个2字节的短片,其值为25,我想放入第一个位置,我不想要“a25defg”。 i want "a" + 0x00 + 0x19 + "defg" (forgive me for that. it was the best way i could think to illustrate what i want). 我想要“a”+ 0x00 + 0x19 +“defg”(请原谅我。这是我能想到的最好的方式来说明我想要的东西)。

is there a slick way to do this using regular C++ strings? 使用常规C ++字符串有一种灵活的方法吗? boost isn't a possibility for me here due to system req's. 由于系统需求,我在这里不可能提升。

from what i see, stringstream may help, but all of the examples i see are converting the number/short/integer into the ascii version of it, and that's not what i want. 从我看到的,stringstream可能会有所帮助,但我看到的所有示例都是将数字/短/整数转换为它的ascii版本,这不是我想要的。

i can easily do this with char arrays, but i was wondering if i can do it with strings. 我可以使用char数组轻松完成此操作,但我想知道我是否可以使用字符串。

thanks. 谢谢。

Its not recommended, but you can just reinterpret it: 它不推荐,但你可以重新解释它:

short something = 25;
string something_string(reinterpret_cast<unsigned char*>(&something),
                        sizeof(something));
// something_string == 19 00

You need to take care of endianess though, (like hton ) 你需要照顾endianess,(像hton

You say it's easy to do with char arrays, but it's just as easy with std::strings! 你说使用char数组很容易,但是使用std :: strings也很容易!

std::string str = "abcdef";
short val = 25;
str[1] = val>>8;
str[2] = val&255;

That works the way you want. 这可以按你想要的方式工作。 If the protocol is big-endian. 如果协议是big-endian。

Edit: 编辑:
This works only if you want to replace existing chars in the string. 仅当您要替换字符串中的现有字符时,此方法才有效。 To create a new string from scratch, or add a value at the end, see Dani's answer. 要从头开始创建新字符串,或在最后添加值,请参阅Dani的答案。

template<size_t N>
void cram_helper( unsigned long long value, char* const dst )
{
    char* p = dst + N;
    while (p > dst) { *--p = value & 0xFF; value >>= 8; }
}

template<typename T>
void cram( T t, char* dst )
{
    cram_helper<sizeof(T)>(t, dst);
}

char buffer[] = { "abcdefg" };
short s = 25;
cram(s, buffer+1);

string buffer1 = "abcdefg";
cram(s, &buffer[1]);

template<size_t N, typename Container>
void cram_helper( unsigned long long value, Container& dst, int initial_index )
{
    char index = initial_index + N;
    while (index > initial_index) { dst[--index] = value & 0xFF; value >>= 8; }
}

template<typename T, typename Container>
void cram( T t, Container& dst, size_t initial_index = 0 )
{
    cram_helper<sizeof(T)>(t, dst, initial_index);
}

std::string buffer2 = "abcdefg";
short s = 25;
cram(s, buffer2, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM