gpt4 book ai didi

C++ 字节数组到 int

转载 作者:搜寻专家 更新时间:2023-10-31 00:26:25 25 4
gpt4 key购买 nike

现在有一个 unsigned char bytes[4] 并且我已经知道字节数组是从 C++ 中的 int 生成的。如何将数组转换回 int

最佳答案

你可以使用 std::memcpy() 来做到这一点:

#include <iostream>
#include <cstring>

int main() {
unsigned char bytes[4]{ 0xdd, 0xcc, 0xbb, 0xaa };

int value;
std::memcpy(&value, bytes, sizeof(int));

std::cout << std::hex << value << '\n';
}

关于C++ 字节数组到 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com