如何将可移植的字符串转换成一种罕见的整数类型? [英] How to portably convert a string into an uncommon integer type?

查看:155
本文介绍了如何将可移植的字符串转换成一种罕见的整数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景资料:如果我想使用的,例如, scanf()的将字符串转换成标准的整数类型,如 uint16_t ,我会使用 SCNu16 < inttypes.h> ,就像这样:

Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t, I’d use SCNu16 from <inttypes.h>, like this:

#include <stdio.h>
#include <inttypes.h>
uint16_t x;
char *xs = "17";
sscanf(xs, "%" SCNu16, &x);

但是,一个更加罕见的整数类型,如将为pid_t 没有任何这样的事情;只有正常整数类型由&LT支持; inttypes.h&GT; 。以其他方式转换,可移植的printf() A 将为pid_t ,我可以把它转换为还会将intmax_t ,并使用 PRIdMAX ,就像这样:

But a more uncommon integer type like pid_t does not have any such thing; only the normal integer types are supported by <inttypes.h>. To convert the other way, to portably printf() a pid_t, I can cast it to intmax_t and use PRIdMAX, like this:

#include <stdio.h>
#include <inttypes.h>
#include <sys/types.h>
pid_t x = 17;
printf("%" PRIdMAX, (intmax_t)x);

但是,似乎没有成为一种可移植 scanf()的将为pid_t 。因此,这是我的问题:怎样可移植做到这一点。

However, there does not seem to be a way to portably scanf() into a pid_t. So this is my question: How to do this portably?

#include <stdio.h>
#include <sys/types.h>
pid_t x;
char *xs = 17;
sscanf(xs, "%u", &x);  /* Not portable! pid_t might not be int! /*

我觉得 scanf()的 ING到<​​code>还会将intmax_t ,然后检查该值在<$ C的$ C>将为pid_t 的限制铸造将为pid_t 过,但似乎没有成为一个方式来获得最大或最小值将为pid_t

I thought of scanf()ing to an intmax_t and then checking that the value is within pid_t’s limits before casting to pid_t, but there does not seem to be a way to get the maximum or minimum values for pid_t.

推荐答案

有一个强大的和便携的解决方案,这是使用 strtoimax()并检查溢出。

There is one robust and portable solution, which is to use strtoimax() and check for overflows.

这就是我解析还会将intmax_t ,从 strtoimax检查错误(),然后还要看看如果它适合在将为pid_t 通过转换它并将其与原还会将intmax_t 值。

That is, I parse an intmax_t, check for an error from strtoimax(), and then also see if it "fits" in a pid_t by casting it and comparing it to the original intmax_t value.

#include <inttypes.h>
#include <stdio.h>
#include <iso646.h>
#include <sys/types.h>
char *xs = "17";            /* The string to convert */
intmax_t xmax;
char *tmp;
pid_t x;                    /* Target variable */

errno = 0;
xmax = strtoimax(xs, &tmp, 10);
if(errno != 0 or tmp == xs or *tmp != '\0'
   or xmax != (pid_t)xmax){
  fprintf(stderr, "Bad PID!\n");
} else {
  x = (pid_t)xmax;
  ...
}

这是不可能使用 scanf()的,因为(正如我在评论说的) scanf()的将不会检测溢出。但我说,没有任何的与strtoll() - 相关职能采取错了一个还会将intmax_t ; strtoimax()呢!

It is not possible to use scanf(), because, (as I said in a comment) scanf() will not detect overflows. But I was wrong in saying that none of the strtoll()-related functions takes an intmax_t; strtoimax() does!

这也将无法正常工作使用别的比 strtoimax(),除非你知道你的整数类型的大小(将为pid_t ,在这种情况下)。

It also will not work to use anything else than strtoimax() unless you know the size of your integer type (pid_t, in this case).

这篇关于如何将可移植的字符串转换成一种罕见的整数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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