c++ - 是否可以从 uint32 读取 s64 值?

标签 c++ c types casting

代码:

s64 end_time;
struct timespec ts;
getrawmonotonic(&ts);
end_time = timespec_to_ns(&ts);

如何删除 end_time 中的前三个字节和最后一个字节?我想将它存储在 uint32 中。有人可以告诉我该怎么做吗??

uint32 latency;
fscanf(fp, "%lu\n", latency);  //fp  is reading the end_time and storing in latency.
latency = (uint32) (latency >> 8) & 0xFFFFFFFF;

从 uint32 读取 s64 值后。我只从中读取了 4 个字节。 是否可以从 uint32 读取 s64 ??

最佳答案

这一行有很多错误:

fscanf(fp, "%lu\n", latency); 
  1. 应该是&latency(您的版本是一个可崩溃的逻辑错误)
  2. uint32 延迟 不一定是 unsigned long

我假设您想要将一个数字读入您希望适合 uint32 的延迟?

执行此操作的唯一可移植方法是使用 ifstream。

这是因为 istream 的运算符 >> 将为非标准 uint32 正确重载。

这样做:

std::ifstream myfile("file_containing_values.txt");
uint32 latency;
myfile >> latency;  // correct overload is selected by the compiler.

关于c++ - 是否可以从 uint32 读取 s64 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23610033/

相关文章:

c - (生命游戏)如何在不检查矩阵外部的情况下循环遍历矩阵的外层,同时检查邻居?

c++ - 当您在 C 中定义一个值时,编译器如何选择数据类型

c++ - QProcess:如何从 pactl 读取输出

c - 为什么数组[i][-1] == 0?如何防止使用 undefined variable ?

c++ - 堆栈抛出内存访问冲突

c - 我是否了解此 malloc/free 组合的内存问题?

types - "Box<Fn() + Send + ' static>”在 Rust 中是什么意思?

typescript - TypeScript 中的类型化枚举实例

c++ - 使用 boost::program_options 指定多个标志

c++ - 在运行时求解 y 立方方程