- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 sscanf
的“%u”格式说明符将十六进制或十进制文本转换为无符号整数。结果不正确,对于值 0x01,sscanf
返回 0 (0)。
根据 C++ Reference , "%u"说明符的定义(突出显示是我的):
i, u Integer 任意数量的数字,前面可以有一个符号(+ 或 -)。默认采用十进制数字 (0-9),但 0 前缀引入八进制数字 (0-7)、和 0x 十六进制数字 (0-f)。
根据 Harbison & Steele,第 3 版:
u 转换 执行无符号十进制转换。...读取的数字格式与 strtol
函数的输入预期相同,base
参数的值为 10;这是一个十进制数字序列,前面可选 - 或 +。
请注意,其中一个定义允许在字符串中指定“0x”。
我正在使用 IAR EW 编译器,编译设置为 C99 方言。
哪个定义对于 C99 是正确的?
我在以下程序中收到两个不同的结果。
这是一个测试程序:
#include <stdio.h>
int main(void)
{
const char text[] = "0x01 1";
unsigned int first_value = 0U;
unsigned int second_value = 0U;
signed int arguments_satisfied = 0;
arguments_satisfied = sscanf(text, "%u %u", &first_value, &second_value);
printf("Arguments scanned: %d, first: %d, second: %d\n",
arguments_satisfied, first_value, second_value);
return EXIT_SUCCESS;
}
最佳答案
根据当前的 C 标准,C11,7.21.6.2/12,只有 %i
从上下文推导基数,所有其他说明符固定基数:
i
Matches an optionally signed integer, whose format is the same as expected for the subject sequence of thestrtol
function with the value 0 for thebase
argument. The corresponding argument shall be a pointer to signed integer.
o
Matches an optionally signed octal integer, whose format is the same as expected for the subject sequence of thestrtoul
function with the value 8 for thebase
argument. The corresponding argument shall be a pointer to unsigned integer.
u
Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of thestrtoul
function with the value 10 for thebase
argument. The corresponding argument shall be a pointer to unsigned integer.
x
Matches an optionally signed hexadecimal integer, whose format is the same as expected for the subject sequence of thestrtoul
function with the value 16 for thebase
argument. The corresponding argument shall be a pointer to unsigned integer.
关于c - sscanf %u 不解释十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18969619/
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我在程序中遇到了以下行。通过阅读手册,我知道 sscanf 从 argv[2] 指向的任何地方复制,但我不确定为什么格式被指定为 %d,同时指定为 %c (我见过其他双引号中包含更多格式说明符的示例)
我可以使用字符指针 (char *) 作为 sscanf() 函数中的输出字符串吗?考虑到我不知道字符串的长度(这就是我使用指针的原因)。 char *name; sscanf(data, "Name
我有一个表单的输入字符串 char *s = "one.two three" 我想把它分成 3 个字符串变量。我在做 sscanf(s, "%s.%s %s", one, two, three); 但
我一直在尝试让 sscanf 使用字符类识别一种相当简单的格式。我注意到,当我为 sscanf 提供 char* 以匹配字符类时,它也会覆盖前一个字节,就好像它需要一个指向 2 个字节的指针一样。 我
我正在将 MAC 地址的字符串表示形式转换为 UINT8 的数组s 定义为 unsigned char 。我很好奇为什么sscanf()当我读入 UINT8 数组时,将读取全 0当我读入常规 32 位
我有一个包含空格和标签的字符串,例如: sp|P02671|FIBA_HUMAN Fibrinogen alpha chain OS=Homo sapiens GN=FGA PE=1 SV=2 我只想
我正在尝试在 C 中使用 sscanf 函数,但它不读取格式是必需的,我已经阅读了该函数的文档并遵循了示例,但它对我来说仍然效果不佳,因此我想要一些建议.. int main() { long i
我需要在c中解析一个格式为“foo=%d”的字符串,我需要检查格式是否正确并读取int值。 我的初始代码是: int foo_set = 0; int foo; if (sscanf(text, "f
在下面的代码中,我想读取十六进制字符串 'a' 中的前 2 个字符,使用 sscanf 将它们转换为相应的字节值并将结果放入 'b'。不应对“a”执行任何修改。 #include #include
我试图找出我应该给 sscanf 的模式。 我有一个字符串 abcde(1GB)。我想提取 1 和 GB。我在用 char list[]= "abcde(1GB)"; int memo
我应该得到一个可以是以下任何格式的输入行: 单词 1 和单词 2 之间必须有空格。 单词 2 和单词 3 之间必须有逗号。 单词 2 和单词 3 之间不一定要有空格,但可以有任意数量的空格。 如何分离
最近发现的对 GTA 冗长加载时间 (1) 的解释表明 sscanf() 的许多实现调用 strlen()在他们的输入字符串上为与其他扫描函数( scanf() , fscanf() ...)共享的内
我需要将字符串分成两部分,字符串的第一列是第一部分,字符串的其余部分是第二部分。第一部分需要存储在 first_str 中,第二部分需要存储在 rest_str 中。 我正在使用 sscanf 来实现
我需要从char数组中提取数字,它以hh:mm的格式存储值(示例20:20) 我尝试使用sscanf函数将hh提取为小时变量,将mm提取为分钟变量。 直到时间类似于0number:0number或如果
所以我想知道 sscanf 在遇到像这样的行时是如何工作的: sscanf(input_string, "%s %s %s", cmd1, cmd2, cmd3); 但是假设 input_string
我在以下代码中遇到 sscanf() 问题: void num_check(const char*ps){ char *ps1=NULL; int number=0; unsigned sum_num
谁能解释一下为什么在下面的代码中没有拆分字符串 #include int main(void) { char name[] = "first:last"; char first[20
我正在尝试解析一个 URL,并编写了这段代码: #include int main() { char host[100]; char port[100]; char path
我正在为我正在制作的程序使用 AT 命令,但在使用 sscanf() 函数解析它们时遇到问题。 例如,如果我执行此命令: "AT\r\r\nOK\r\n" 我想:* 在第一次调用时,仅获取“AT”部分
我是一名优秀的程序员,十分优秀!