【问题标题】:is it possible to read s64 value from uint32?是否可以从 uint32 读取 s64 值?
【发布时间】:2014-06-29 20:59:36
【问题描述】:

代码:

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 ??

【问题讨论】:

  • 或许(end_time >> 8) & 0xffffffff?
  • s64uint32 都不是标准的 C 或 C++ 类型。我假设它们分别是有符号的 64 位整数和无符号的 32 位整数,但只有给出的信息它们可以是任何东西。
  • 自 C99 以来的标准方式是 int32_tunsigned int64_t
  • @LưuVĩnhPhúc 而不是 uint32_tint64_t

标签: c++ c types casting


【解决方案1】:

这一行有很多错误:

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

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

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

这是因为 istream 的 operator>> 将正确地为非标准 uint32 重载。

这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2018-06-26
    • 2017-04-24
    • 2013-02-07
    • 1970-01-01
    • 2021-12-29
    • 2011-01-10
    相关资源
    最近更新 更多