读取十六进制数并验证其在C中的位数(Read an hexadecimal number and validate its number of digits in C)

编程入门 行业动态 更新时间:2024-10-27 20:37:37
读取十六进制数并验证其在C中的位数(Read an hexadecimal number and validate its number of digits in C)

我需要在C中读取带有八位数的十六进制数字,我需要对其进行验证。 如果小于8位,则填写“0”。 如果它超过八位数,请打印“请插入有效数字”之类的消息。

我正在做这样的事情:

unsigned int number; printf("\nIntroduzca un número: "); scanf("%08x", &number);

但是使用“%08x”我可以填写“0”,但我不知道插入的数字是否超过8位并打印错误信息。

我想把它读成字符串,但后来我需要对它进行位操作并以十六进制和二进制形式打印结果,所以我不知道将数字读成字符串是否是一个好主意。

我怎么解决这个问题?

I need to read an hexadecimal number with eight digits in C and I need to validate it. If it has less than eight digits fill with "0". If it has more than eight digits, print a message like "please insert a valid number".

I'm doing something like this:

unsigned int number; printf("\nIntroduzca un número: "); scanf("%08x", &number);

But using "%08x" I can fill with "0" but I can't know if the inserted number has more than eight digits and print the error message.

I thought of reading it as a string but later I need to do bit operations on it and print the result in hexadecimal and binary, so I don't know if it's a good idea read the numbers as strings.

How can I solve this problem?

最满意答案

scanf()很少是用户输入的最佳选择。 最好使用fgets()读取一行,然后使用其他代码来解析字符串。

char buf[100]; while (fgets(buf, sizeof buf, stdin)) { // code read the text input into a string, now do something with it.

代码正在查找八个十六进制数字,OP在右侧轨道上使用scanf("%08x"...此处不需要0只会将输入限制为8位。使用" "跳过任何可选项空格。使用"%n"记录解析的字符数。

unsigned number = 0; int n = 0; // If any length (up to 8) hex text was found ... // ... and test if `buf[n]` is the end of the string or maybe that 9 digit? if (sscanf(buf, "%8x %n", &number, &n) == 1) && buf[n] == '\0') { printf("Success :%08x\n", number); } else { fprintf(stderr, "please insert a valid number\n"); } }

如果代码需要确保文本不包含像"0x123"这样的"0x123" , "0x123"更多的工作。 各种方法。

使用*scanf()说明符,使用"%*[0123456789abcdefABCDEF]"扫描字符串中的十六进制数字。 *表示不保存,只是扫描。

int n = 0; // If any length (up to 8) hex text was found ... // ... and test if `buf[n]` is the end of the string or maybe that 9 digit? sscanf(buf, "%*8[0123456789abcdefABCDEF] %n", &n); if (n > 0 && buf[n] == '\0') { unsigned long number = strtoul(buf, NULL, 16); printf("Success :%08lx\n", number); } else { fprintf(stderr, "please insert a valid number\n"); } }

一种聪明的方法是一起避免使用*scanf() ,并在要解析的字符串前加上"0x" ,并仅使用strtol()进行解析。

char buf[100]; while (fgets(&buf[2], sizeof buf - 2, stdin)) { char *endptr; buf[0] = '0'; buf[1] = 'x'; unsigned long number = strtoul(buf, &endptr, 16); int length = (int) (endptr - buf); if (length < 3 || length > (2 + 8) || *endptr != '\n') { fprintf(stderr, "please insert a valid number\n"); } else { printf("Success :%08lx\n", number); } }

Rarely is scanf() the best choice to user input. Better to use fgets() to read a line and then additional code to parse the string.

char buf[100]; while (fgets(buf, sizeof buf, stdin)) { // code read the text input into a string, now do something with it.

Code is looking for eight hexadecimal digits and OP is on the right track with scanf("%08x".... 0 is not needed here, just 8 will do to limit the input to 8 digits. Use " " to skip any optional white space. Use "%n" to record the number of characters parsed.

unsigned number = 0; int n = 0; // If any length (up to 8) hex text was found ... // ... and test if `buf[n]` is the end of the string or maybe that 9 digit? if (sscanf(buf, "%8x %n", &number, &n) == 1) && buf[n] == '\0') { printf("Success :%08x\n", number); } else { fprintf(stderr, "please insert a valid number\n"); } }

If code needs to insure text did not contain a leading like "0x123", more work is needed. Various approaches.

Sticking with *scanf() specifiers, use "%*[0123456789abcdefABCDEF]" to scan a string for hex digits. The * means to not save, just scan.

int n = 0; // If any length (up to 8) hex text was found ... // ... and test if `buf[n]` is the end of the string or maybe that 9 digit? sscanf(buf, "%*8[0123456789abcdefABCDEF] %n", &n); if (n > 0 && buf[n] == '\0') { unsigned long number = strtoul(buf, NULL, 16); printf("Success :%08lx\n", number); } else { fprintf(stderr, "please insert a valid number\n"); } }

A clever way is to avoid *scanf() all together and prepend a "0x" to the string to be parsed and use strtol() only for parsing.

char buf[100]; while (fgets(&buf[2], sizeof buf - 2, stdin)) { char *endptr; buf[0] = '0'; buf[1] = 'x'; unsigned long number = strtoul(buf, &endptr, 16); int length = (int) (endptr - buf); if (length < 3 || length > (2 + 8) || *endptr != '\n') { fprintf(stderr, "please insert a valid number\n"); } else { printf("Success :%08lx\n", number); } }

更多推荐

本文发布于:2023-07-26 17:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位数   Read   十六进制数   hexadecimal   digits

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!