如何使用 sprintf 显示 64 位无符号整数? [英] How do I display a 64bit unsigned integer with sprintf?

查看:147
本文介绍了如何使用 sprintf 显示 64 位无符号整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的腻子控制台上显示一个 32 位计时器值.计时器包括自 stm32wb55 启动以来的时间(以微秒为单位).使用以下代码,它可以像我想要的那样工作:

I'm displaying a 32bit timer value on my putty-console. The timer includes the time in microseconds since startup of my stm32wb55. With following code, it works exaclty like I want:

uint32_t time_micro32

sprintf((char*)buf,
          "Time: %lu \r\n",
         (time_micro32));

但现在,我想用 uint64_t time_micro64 以 64 位分辨率显示时间.我尝试了很多,但没有任何效果.有人可以帮我吗?我正在 STM32CubeIDE 中编程

But now, I want to display the time in 64bit resolution with uint64_t time_micro64. I tried many, but nothing works. Can anybody help me, please? I´m programming in STM32CubeIDE

推荐答案

我正在 STM32CubeIDE 中编程

I´m programming in STM32CubeIDE

所以你很可能在它的nano"中使用 newlibversion - 较小版本的 newlib,不处理打印 64 位变量.

So you are using most probably using newlib in it's "nano" version - a smaller version of newlib, which does not handle printing 64-bit variables.

与完整的 newlib 版本(或使用其他 C 标准库)链接,代码将正常工作.搜索您的 gui - 应该有一个选项,例如使用标准库的 -nano 版本";或将 -specs=nano.specs 传递给编译器".取消选中该选项.确保 -lnano-specs=nano.specs 或类似选项没有传递给编译器.之后,您可以使用 %llu"%"PRIu64.

Link with full newlib version (or use other C standard library) and the code will work fine. Search your gui - there should be an option like "use -nano version of the standard library" or "pass -specs=nano.specs to the compiler". Uncheck that option. Make sure that -lnano nor -specs=nano.specs or similar options are not passed to the compiler. After that, you can use %llu or "%"PRIu64.

更喜欢使用 snprintf 而不是 sprintf.

Prefer to use snprintf instead of sprintf.

还有其他选择吗?

滚动您自己的实现,将 64 位变量转换为字符串 - 编写这样的函数非常简单.然后使用此函数并打印结果字符串.我有这样的功能.

Roll your own implementation that would convert 64-bit variable into a string - writing such function is very trivial. Then use this function and print the resulting string. I have such function.

这篇关于如何使用 sprintf 显示 64 位无符号整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆