简体   繁体   English

uint8_t、uint16_t、...的格式说明符?

[英]Format specifiers for uint8_t, uint16_t, …?

If I have an integer variable I can use sscanf as shown below by using the format specifier %d.如果我有一个整数变量,我可以通过使用格式说明符 %d 来使用sscanf ,如下所示。

sscanf (line, "Value of integer: %d\n", &my_integer);

Where can I find format specifiers for uint8_t , uint16_t , uint32_t and uint64_t ?在哪里可以找到uint8_tuint16_tuint32_tuint64_t格式说明符?

uint64_t has probably %lu. uint64_t 可能有 %lu。

They are declared in <inttypes.h> as macros: SCNd8, SCNd16, SCNd32 and SCNd64.它们在<inttypes.h>中声明为宏:SCNd8、SCNd16、SCNd32 和 SCNd64。 Example (for int32_t):示例(对于 int32_t):

sscanf (line, "Value of integer: %" SCNd32 "\n", &my_integer);

Their format is PRI (for printf)/SCN (for scan) then o, u, x, X d, i for the corresponding specifier then nothing, LEAST, FAST, MAX then the size (obviously there is no size for MAX).它们的格式是 PRI(用于 printf)/SCN(用于扫描)然后是 o, u, x, X d, i 用于相应的说明符然后什么都没有,LEAST、FAST、MAX 然后是大小(显然 MAX 没有大小)。 Some other examples: PRIo8, PRIuMAX, SCNoFAST16.其他一些示例:PRIo8、PRIuMAX、SCNoFAST16。

Edit: BTW a related question asked why that method was used.编辑:顺便说一句,一个相关的问题询问了为什么使用该方法。 You may find the answers interesting.你可能会发现答案很有趣。

As others said, include <stdint.h> header that defines the format macros.正如其他人所说,包括定义格式宏的<stdint.h>标头。 In C++, however, define __STDC_FORMAT_MACROS prior to including it.但是,在 C++ 中,在包含它之前定义__STDC_FORMAT_MACROS From stdint.h:来自 stdint.h:

/* The ISO C99 standard specifies that these macros must only be
   defined if explicitly requested.  */
#if !defined __cplusplus || defined __STDC_FORMAT_MACROS

According to 7.19.6 Formatted input/output functions of ISO/IEC 9899:TC2 , there are no such format specifiers (so I doubt there any for C++2003).根据ISO/IEC 9899:TC2 的7.19.6 Formatted input/output functions ,没有这样的格式说明符(所以我怀疑 C++2003 有没有)。 Even though there are some #define-macros available in C99's inttypes.h , cinttypes and inttypes.h are not part of the current standard.尽管 C99 的inttypes.h有一些 #define-macros 可用,但cinttypesinttypes.h不是当前标准的一部分。 Of course, fixed-size integer types are non-standard as well.当然,固定大小的整数类型也是非标准的。

Anyways, I seriously recommemend using streams instead:无论如何,我强烈建议改用流:

<any_type> x;
f >> x;

and be done.并完成。 Eg:例如:

std::stringstream ss;
uint32_t u;
std::cin >> u;

This has the advantage that one time in the future, changing the type of the variable does not cause a cascade of subtle bugs and undefined behaviour.这样做的好处是,将来更改变量的类型不会导致一连串微妙的错误和未定义的行为。

The right format to read a uint64_t (typedef unsigned long long int) is, with scanf and not sscanf , "%" SCNu64 and for print is also SCNu64 Example.读取uint64_t (typedef unsigned long long int) 的正确格式是,使用scanf而不是sscanf"%" SCNu64和打印也是SCNu64示例。 in your code you read for example my_integer variable, then you do scanf ("Value of integer:%" SCNu64, & my_integer);在您的代码中,您读取例如 my_integer 变量,然后执行scanf ("Value of integer:%" SCNu64, & my_integer); and to write the same but with printf.并使用 printf 编写相同的内容。

You can check types in <cstdint> for C++ or in <types.h> for C. Then you can specify format which is currently compiled on your machine.您可以在<cstdint>检查 C++ 的类型或在<types.h>检查 C 的类型。然后您可以指定当前在您的机器上编译的格式。 If variable is unsigned then you need to use %u , if variable is signed , then use %d .如果变量是unsigned那么你需要使用%u ,如果变量是有signed ,那么使用%d For variables size lower than 32 bytes you don't have to specify long prefix.对于小于 32 字节的变量大小,您不必指定长前缀。

In my configuration I have 64 bit program, so my uint64_t is compiled as unsigned long long int , so it means to use %llu .在我的配置中,我有 64 位程序,所以我的uint64_t被编译为unsigned long long int ,所以这意味着使用%llu If your program will be compiled in 32 bit you can use %lu because uint64_t will be compiled as unsigned long int如果您的程序将被编译为 32 位,您可以使用%lu因为uint64_t将被编译为unsigned long int

But who is using x86 still?但是谁还在使用 x86? :), so generally: :),所以一般来说:

uint8_t, uint16_t, uint32_t - %u
uint64_t - %llu
int8_t, int16_t, int32_t - %d
int64_t -%lld

In C, the header is <inttypes.h> , and formats such as SCNX8, SCNd16.在 C 中,头是<inttypes.h> ,格式如 SCNX8、SCNd16。

The same header probably works for C++ too.相同的头文件可能也适用于 C++。

Refer to this for sscanf usage.请参阅为sscanf的用法。

These datatypes are defined in stdint.h.这些数据类型在 stdint.h 中定义。 Refer here for stdint.h请参阅此处了解 stdint.h

Shash沙什

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM