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

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

我有一个 unsigned char 向量,我在 C++ 中复制字节。我将所有原始类型转换为字节并复制到这个 char 向量(在 C++ 中被解释为字节)。现在我也在复制字符串。但我不确定我是否将字符串转换为字节。如果您在我 printing the vector of unsigned char 时查看我的输出,我正在打印来自 double int float 的字节,但我正在打印变量的真实字符串 testString。所以我想我没有在我的 unsigned char 向量上插入这个 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 vector\n" << 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 vector\n" << 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 value\n" << 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 %s\n", 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,接下来的 30 个字节来自 char[]。这就是 char[] 的存储方式!您的代码正在执行您期望的操作。

当然,您不能完全依赖它以这种方式执行此操作;那是未定义的行为。

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