uint8_t, uint16_t,…?

时间:2022-09-06 12:52:40

If I have an integer variable I can use sscanf as shown below by using the format specifier %d.

如果我有一个整型变量,我可以使用sscanf,如下所示,使用format specifier %d。

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_t、uint16_t、uint32_t和uint64_t的格式说明符?

uint64_t has probably %lu.

uint64_t可能%。

6 个解决方案

#1


79  

They are declared in <inttypes.h> as macros: SCNd8, SCNd16, SCNd32 and SCNd64. Example (for int32_t):

它们在 中被声明。h>作为宏:SCNd8、SCNd16、SCNd32和SCNd64。示例(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). Some other examples: PRIo8, PRIuMAX, SCNoFAST16.

它们的格式是PRI(用于printf)/SCN(用于扫描),然后o, u, x, x d, i对应的说明符,然后没有任何东西,至少,快速,最大值,然后是大小(显然,MAX没有大小)。还有一些例子:PRIo8, PRIuMAX, SCNoFAST16。

Edit: BTW a related question asked why that method was used. You may find the answers interesting.

编辑:顺便问一下,为什么使用这个方法?你会发现答案很有趣。

#2


7  

As others said, include <stdint.h> header that defines the format macros. In C++, however, define __STDC_FORMAT_MACROS prior to including it. From stdint.h:

正如其他人所说,包括 标题。但是,在c++中,在包括它之前定义__STDC_FORMAT_MACROS。从stdint.h: 。定义格式宏的h>

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

#3


2  

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). Even though there are some #define-macros available in C99's inttypes.h, cinttypes and inttypes.h are not part of the current standard. Of course, fixed-size integer types are non-standard as well.

根据ISO/ iec9899:TC2的格式输入/输出功能,没有这样的格式说明(所以我怀疑c++ 2003是否有)。尽管C99的inttypes中有一些#define-宏。h,cinttypes inttypes。h不是当前标准的一部分。当然,固定大小的整数类型也是不标准的。

Anyways, I seriously recommemend using streams instead:

无论如何,我认真地建议用流来代替:

<any_type> x;
f >> x;

and be done. E.g.:

和做。例如:

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.

这在将来有一段时间的优势,改变变量的类型不会导致细微的错误和未定义的行为。

#4


0  

In C, the header is <inttypes.h>, and formats such as SCNX8, SCNd16.

在C中,header是 ,以及SCNX8、SCNd16等格式。 。h>

The same header probably works for C++ too.

同样的标题可能也适用于c++。

#5


0  

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. in your code you read for example my_integer variable, then you do scanf ("Value of integer:%" SCNu64, & my_integer); and to write the same but with printf.

读取uint64_t (typedef unsigned long long int)的正确格式是,使用scanf而不是sscanf,“%”SCNu64和打印也是SCNu64示例。在您的代码中,您读取的是my_integer变量,然后是scanf(“整数的值:%”SCNu64, & my_integer);和printf一样。

#6


-1  

Refer to this for sscanf usage.

关于sscanf的使用,请参考这个。

These datatypes are defined in stdint.h. Refer here for stdint.h

这些数据类型在stdint.h中定义。在这里引用stdint.h

Shash

优质棉细布

#1


79  

They are declared in <inttypes.h> as macros: SCNd8, SCNd16, SCNd32 and SCNd64. Example (for int32_t):

它们在 中被声明。h>作为宏:SCNd8、SCNd16、SCNd32和SCNd64。示例(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). Some other examples: PRIo8, PRIuMAX, SCNoFAST16.

它们的格式是PRI(用于printf)/SCN(用于扫描),然后o, u, x, x d, i对应的说明符,然后没有任何东西,至少,快速,最大值,然后是大小(显然,MAX没有大小)。还有一些例子:PRIo8, PRIuMAX, SCNoFAST16。

Edit: BTW a related question asked why that method was used. You may find the answers interesting.

编辑:顺便问一下,为什么使用这个方法?你会发现答案很有趣。

#2


7  

As others said, include <stdint.h> header that defines the format macros. In C++, however, define __STDC_FORMAT_MACROS prior to including it. From stdint.h:

正如其他人所说,包括 标题。但是,在c++中,在包括它之前定义__STDC_FORMAT_MACROS。从stdint.h: 。定义格式宏的h>

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

#3


2  

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). Even though there are some #define-macros available in C99's inttypes.h, cinttypes and inttypes.h are not part of the current standard. Of course, fixed-size integer types are non-standard as well.

根据ISO/ iec9899:TC2的格式输入/输出功能,没有这样的格式说明(所以我怀疑c++ 2003是否有)。尽管C99的inttypes中有一些#define-宏。h,cinttypes inttypes。h不是当前标准的一部分。当然,固定大小的整数类型也是不标准的。

Anyways, I seriously recommemend using streams instead:

无论如何,我认真地建议用流来代替:

<any_type> x;
f >> x;

and be done. E.g.:

和做。例如:

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.

这在将来有一段时间的优势,改变变量的类型不会导致细微的错误和未定义的行为。

#4


0  

In C, the header is <inttypes.h>, and formats such as SCNX8, SCNd16.

在C中,header是 ,以及SCNX8、SCNd16等格式。 。h>

The same header probably works for C++ too.

同样的标题可能也适用于c++。

#5


0  

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. in your code you read for example my_integer variable, then you do scanf ("Value of integer:%" SCNu64, & my_integer); and to write the same but with printf.

读取uint64_t (typedef unsigned long long int)的正确格式是,使用scanf而不是sscanf,“%”SCNu64和打印也是SCNu64示例。在您的代码中,您读取的是my_integer变量,然后是scanf(“整数的值:%”SCNu64, & my_integer);和printf一样。

#6


-1  

Refer to this for sscanf usage.

关于sscanf的使用,请参考这个。

These datatypes are defined in stdint.h. Refer here for stdint.h

这些数据类型在stdint.h中定义。在这里引用stdint.h

Shash

优质棉细布