C-转换为uint64_t的字符串并进行错误处理 [英] C - string to uint64_t casting with error handling

查看:460
本文介绍了C-转换为uint64_t的字符串并进行错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序将字符串转换为 uint64_t 类型并获取错误(如果有)。

This program is to cast strings to uint64_t types and get the errors if any.

它应该输出,在这种情况下,出现2个错误(溢出和负数),均未出现。而且,它不能正确地转换字符串之一。

It should output, in this case, 2 errors (overflow and negative number), none of which appear. Also, it doesn't properly cast one of the strings.

任何帮助将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>

int func(const char *buff) {
    char *end;
    int si;

    errno = 0;

    const uint64_t sl = strtoull(buff, &end, 10);

    if (end == buff) {
    fprintf(stderr, "%s: not a decimal number\n", buff);
    } else if ('\0' != *end) {
    fprintf(stderr, "%s: extra characters at end of input: %s\n", buff, end);
    } else if ((sl < 0 || ULONG_MAX == sl) && ERANGE == errno) {
    fprintf(stderr, "%s out of range of type uint64_t\n", buff);
    } else if (sl > ULONG_MAX) {
    fprintf(stderr, "%ld greater than ULONG_MAX\n", sl);
    } else if (sl < 0) {
     fprintf(stderr, "%ld negative\n", sl);
    } else {
    si = (int)sl;
    }

    return si;
}

int main()
{

    printf("%d\n", func("123456789123465789"));
    printf("%d\n", func("123456789123465789123465789"));
    printf("%d\n", func("-1"));


    return 0;
}


推荐答案

strtoull(); 允许以'-'开头的输入。该函数转换文本的数字部分,然后取反,结果仍然是 unsigned 整数。因此,代码无法使用 strtoull()的结果来确定文本是否如 uint64_t sl那样表示负数; sl< 0 始终为假。

strtoull(); allows input that begin with '-'. The function converts the numeric portion of the text and then negates it, the result is still an unsigned integer. So code cannot use the result of strtoull() to determine if the text represented a negative number as with uint64_t sl;, sl < 0 is always false.

代码需要在其他代码中检测到'-'

Code needs to detect the '-' in other ways.

我希望函数返回 uint64_t

#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>

// int func(const char *buff) {
uint64_t func(const char *buff) {
  const char *s = buff;
  // consume leading whitespace
  while (isspace((unsigned char ) *s)) {
    s++;
  }
  int sign = *s;
  if (sign == '-' || sign == '+') {
    s++;
  }
  // Now code knows it the text is "negative"

  // rest of OP's code needs a some work
  char *end;
  errno = 0;
  unsigned long long sl = strtoull(s, &end, 10);

  if (end == s) {
    fprintf(stderr, "%s: not a decimal number\n", buff);
  } else if ('\0' != *end) {
    fprintf(stderr, "%s: extra characters at end of input: %s\n", buff, end);
    // } else if ((sl < 0 || ULONG_MAX == sl) && ERANGE == errno) {
  } else if (sign == '-') {
    fprintf(stderr, "%s negative\n", buff);
    sl = 0;
    errno = ERANGE;
  } else if (ERANGE == errno) {
    fprintf(stderr, "%s out of range of type uint64_t\n", buff);

    // Only needed on rare machines
#if ULLONG_MAX > UINT64_MAX
    else if (sl > UINT64_MAX) {
      fprintf(stderr, "%s out of range of type uint64_t\n", buff);
      sl = UINT64_MAX;
      errno = ERANGE;
    }
#endif

  }
  return (uint64_t) sl;
}

OP返回 int 很奇怪。保留将 main()更新为OP。

OP's returning of an int is strange. Leave updating main() to OP.

这篇关于C-转换为uint64_t的字符串并进行错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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