在这种情况下,我真的复制了字节还是复制了字符?
Am I really copying the bytes or am I copying characters in this case?
我有一个无符号字符向量,我在其中复制C++字节。我将所有基元类型转换为字节并复制到这个 char 向量(在 C++ 中被解释为字节(。现在我也在复制字符串。但我不确定我是否将字符串转换为字节。如果你看看我的输出,当我printing the vector
无符号字符时,我正在打印来自double
int
float
的字节,但我正在打印我的变量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 个字节来自double
s,接下来的 30 个字节来自char[]
。这就是char[]
的存储方式!您的代码正在执行您期望的操作。
当然,你不能完全依赖它以这种方式做到这一点;这是未定义的行为。
我觉得你期待的是这样的:128565TESTSTRING
其中12
、85
和65
是经度、纬度和高度的值。好吧,这不会发生,因为您在数据中写了12
,而不是"12"
;因此,它将返回 ASCII 代码为12
的字符。也许你可以用类似sprintf()
的东西来代替。
相关文章:
- 在这种情况下,我真的复制了字节还是复制了字符?
- 如何返回/复制unique_ptr<无符号字符[]>的值?
- 使用动态分配将 char* 复制到另一个字符**
- 库达如何将字符**从内核复制到主机
- 将字符向量复制到另一个向量
- 为什么我的代码在尝试复制字符数组时引发 C6386 错误?
- 使用 memcpy 函数C++复制字符数组
- 如何使用strncpy_s复制字符数组
- 在结构之间复制字符数据
- std::逐字节复制字符向量
- 比strncpy更好地复制字符阵列的部分方法
- 使用"="运算符复制字符* 内容
- 用 0 复制字符数组中的短整数
- 使用指针重新复制字符数组并修剪前导空格
- C++ 使用 char* 复制字符数组(无字符串库)
- 复制 c++ 字符指针
- 使用rd_buf复制C++字符串会更改const对象
- 如何从字符串指针复制字符到字符串向量
- 字符串字面值操作-复制字符
- 在使用std::copy而不是std::memcpy复制字符时避免libc函数调用