在这种情况下,我真的复制了字节还是复制了字符?

Am I really copying the bytes or am I copying characters in this case?

本文关键字:复制 字符 字节 这种情况下 真的      更新时间:2023-10-16

我有一个无符号字符向量,我在其中复制C++字节。我将所有基元类型转换为字节并复制到这个 char 向量(在 C++ 中被解释为字节(。现在我也在复制字符串。但我不确定我是否将字符串转换为字节。如果你看看我的输出,当我printing the vector无符号字符时,我正在打印来自doubleintfloat的字节,但我正在打印我的变量testString的真实字符串。所以我想我没有在我的无符号字符向量上插入这个testString的字节。我应该怎么做? 谢谢

const std::string lat = "lat->", alt = "alt->", lon = "lon->", testString = "TEST-STRING";
double latitude = 10.123456;
double longitude = 50.123456;
double altitude = 1.123456;
std::vector<unsigned char> result(
sizeof(latitude) + sizeof(longitude) + sizeof(altitude) + testString.length());
std::cout << "copying to the vector" << std::endl;
memcpy(result.data(), &longitude, sizeof(longitude));
memcpy(result.data() + sizeof(longitude), &latitude, sizeof(latitude));
memcpy(result.data() + sizeof(longitude) + sizeof(latitude), &altitude, sizeof(altitude));
memcpy(result.data() + sizeof(longitude) + sizeof(latitude) + sizeof(altitude), testString.c_str(),
testString.length() + 1);
std::cout << "copied to the vectorn" << std::endl;
std::cout << "printing the vector" << std::endl;
for (unsigned int j = 0; j < result.size(); j++) {
std::cout << result[j];
}
std::cout << std::endl;
std::cout << "printed the vectorn" << std::endl;
// testing converting back ...................
std::cout << "printing back the original value" << std::endl;
double dLat, dLon, dAlt;
std::string value;
memcpy(&dLon, result.data(), sizeof(longitude));
memcpy(&dLat, result.data() + sizeof(longitude), sizeof(latitude));
memcpy(&dAlt, result.data() + sizeof(longitude) + sizeof(latitude), sizeof(altitude));
value.resize(testString.length());
memcpy(&value[0], result.data() + sizeof(longitude) + sizeof(latitude) + sizeof(altitude),
sizeof(value.data()) + testString.size());
std::cout << alt << dAlt;
std::cout << lat << dLat;
std::cout << lon << dLon;
std::cout << " " << value << std::endl;
std::cout << "printed back the original valuen" << std::endl; 

输出:

copying to the vector
copied to the vector
printing the vector
[?�gI@m���5?$@l������?TEST-STRING
printed the vector
printing back the original value
alt->1.12346lat->10.1235lon->50.1235 TEST-STRING
printed back the original value

你的代码没有问题!您正在打印变量的实际字节数。double中的字节不能真正解释为文本字符串(至少,如果你这样做就没有意义了(,但文本字符串中的字节可以,产生你所看到的。

假设你有以下代码(实际上只是伪装的 C(:

#include <cstdio>
int main(int argc, char *argv[]) {
struct {
double latitude;
double longitude;
char name[30];
} structure = {
53.6344,
126.5223167,
"Keyboard Mash"
};
printf("%f %f %sn", structure.latitude, structure.longitude, structure.name);
for (size_t i = 0; i < sizeof(structure); i += 1) {
printf("%c", ((char*)&structure)[i]);
}
printf("n");
}

此代码将(可能(打印:

53.6344 126.5223167 Keyboard Mash
����������������Keyboard Mash�����������������

前 16 个字节来自doubles,接下来的 30 个字节来自char[]。这就是char[]的存储方式!您的代码正在执行您期望的操作。

当然,你不能完全依赖它以这种方式做到这一点;这是未定义的行为。

我觉得你期待的是这样的:128565TESTSTRING其中128565是经度、纬度和高度的值。好吧,这不会发生,因为您在数据中写了12,而不是"12";因此,它将返回 ASCII 代码为12的字符。也许你可以用类似sprintf()的东西来代替。