C++ 字符串到字节数组的转换和加法
C++ String to byteArray Convertion and Addition
我有一个要转换为 byteArray 的字符串,然后我希望将这个 byteArray 添加到另一个 byteArray,但是在那个 byteArray 的开头。
假设这是我的字符串
string suffix = "$PMARVD";
这是我现有的 byteArray(忽略那里的对象,它是一个现在不相关的 .proto 对象):
int size = visionDataMsg.ByteSize(); // see how big is it
char* byteArray = new char[size]; //create a bytearray of that size
visionDataMsg.SerializeToArray(byteArray, size); // serialize it
所以我想做的是这样的:
char* byteArrayforSuffix = suffix.convertToByteArray();
char* byteArrayforBoth = byteArrayforSuffix + byteArray;
有没有在 C++ 中执行此操作的?
编辑:我应该补充一点,在连接操作之后,完整的byteArrayforBoth将在:
中处理
// convert bytearray to vector
vector<unsigned char> byteVector(byteArrayforBoth, byteArrayforBoth + size);
std::string
背后的整个想法是用管理所有内容的 class 包装 C 样式字符串(空终止 charcaters/bytes 数组)。
你可以使用std::string::data
方法来超出内部字符数组。示例:
std::string hello ("hello") , world(" world");
auto helloWorld = hello + world;
const char* byteArray = helloWorld.data();
编辑:
ByteArray 是char[]
或unsigned char[]
的内置类型,不像Java 或C#,你不能只是"append" 内置字节数组到另一个。正如您所建议的,您只需要一个无符号字符向量。在这种情况下,我会简单地创建一个利用 push_back
:
的效用函数
void appendBytes(vector<unsigend char>& dest,const char* characterArray,size_t size){
dest.reserve(dest.size() + size);
for (size_t i=0;i<size;i++){
dest.push_back(characterArray[i]);
}
}
现在,使用您提供的对象:
std::vector<unsigned char> dest;
appendBytes(dest, suffix.data(),suffix.size());
auto another = visionDataMsg.SerializeToArray(byteArray, size);
appendBytes(dest,another,size);
废弃内置数组。你有向量。这是完全可用的类型安全解决方案,我花了 3 分钟来输入:
int size = visionDataMsg.ByteSize(); // see how big is it
std::vector<char> byteArray(size);
visionDataMsg.SerializeToArray(&byteArray[0], size); // serialize it
std::string str("My String");
byteArray.reserve(byteArray.size() + str.size());
std::copy(str.begin(), str.end(), std::back_inserter(byteArray));
我有一个要转换为 byteArray 的字符串,然后我希望将这个 byteArray 添加到另一个 byteArray,但是在那个 byteArray 的开头。
假设这是我的字符串
string suffix = "$PMARVD";
这是我现有的 byteArray(忽略那里的对象,它是一个现在不相关的 .proto 对象):
int size = visionDataMsg.ByteSize(); // see how big is it
char* byteArray = new char[size]; //create a bytearray of that size
visionDataMsg.SerializeToArray(byteArray, size); // serialize it
所以我想做的是这样的:
char* byteArrayforSuffix = suffix.convertToByteArray();
char* byteArrayforBoth = byteArrayforSuffix + byteArray;
有没有在 C++ 中执行此操作的?
编辑:我应该补充一点,在连接操作之后,完整的byteArrayforBoth将在:
中处理// convert bytearray to vector
vector<unsigned char> byteVector(byteArrayforBoth, byteArrayforBoth + size);
std::string
背后的整个想法是用管理所有内容的 class 包装 C 样式字符串(空终止 charcaters/bytes 数组)。
你可以使用std::string::data
方法来超出内部字符数组。示例:
std::string hello ("hello") , world(" world");
auto helloWorld = hello + world;
const char* byteArray = helloWorld.data();
编辑:
ByteArray 是char[]
或unsigned char[]
的内置类型,不像Java 或C#,你不能只是"append" 内置字节数组到另一个。正如您所建议的,您只需要一个无符号字符向量。在这种情况下,我会简单地创建一个利用 push_back
:
void appendBytes(vector<unsigend char>& dest,const char* characterArray,size_t size){
dest.reserve(dest.size() + size);
for (size_t i=0;i<size;i++){
dest.push_back(characterArray[i]);
}
}
现在,使用您提供的对象:
std::vector<unsigned char> dest;
appendBytes(dest, suffix.data(),suffix.size());
auto another = visionDataMsg.SerializeToArray(byteArray, size);
appendBytes(dest,another,size);
废弃内置数组。你有向量。这是完全可用的类型安全解决方案,我花了 3 分钟来输入:
int size = visionDataMsg.ByteSize(); // see how big is it
std::vector<char> byteArray(size);
visionDataMsg.SerializeToArray(&byteArray[0], size); // serialize it
std::string str("My String");
byteArray.reserve(byteArray.size() + str.size());
std::copy(str.begin(), str.end(), std::back_inserter(byteArray));