C语言 如何将字符串转换为 int64_t?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17002969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:38:09  来源:igfitidea点击:

How to convert string to int64_t?

cunixint64

提问by pmichna

How to convert program parameter from argvto int64_t? atoi()is suitable only for 32 bit integers.

如何将程序参数从 转换argvint64_tatoi()仅适用于 32 位整数。

采纳答案by chux - Reinstate Monica

A C99 conforming attempt.

符合 C99 的尝试。

[edit] employed @R. correction

[编辑] 使用@R。更正

// Note: Typical values of SCNd64 include "lld" and "ld".
#include <inttypes.h>
#include <stdio.h>

int64_t S64(const char *s) {
  int64_t i;
  char c ;
  int scanned = sscanf(s, "%" SCNd64 "%c", &i, &c);
  if (scanned == 1) return i;
  if (scanned > 1) {
    // TBD about extra data found
    return i;
    }
  // TBD failed to scan;  
  return 0;  
}

int main(int argc, char *argv[]) {
  if (argc > 1) {
    int64_t i = S64(argv[1]);
    printf("%" SCNd64 "\n", i);
  }
  return 0;
}

回答by Ahmed Masud

There are a few ways to do it:

有几种方法可以做到:

  strtoll(str, NULL, 10);

This is POSIX C99 compliant.

这是符合 POSIX C99 的。

you can also use strtoimax; which has the following prototype:

你也可以使用 strtoimax;它具有以下原型:

 strtoimax(const char *str, char **endptr, int base);

This is nice because it will always work with the local intmax_t ... This is C99 and you need to include <inttypes.h>

这很好,因为它始终与本地 intmax_t 一起使用...这是 C99,您需要包括 <inttypes.h>

回答by davidvandebunte

Users coming from a web search should also consider std::stoll.

来自网络搜索的用户也应该考虑std::stoll.

It doesn't strictly answer this original question efficiently for a const char*but many users will have a std::stringanyways. If you don't care about efficiency you should get an implicit conversion (based on the user-defined conversion using the single-argument std::stringconstructor) to std::stringeven if you have a const char*.

它并没有严格有效地回答这个原始问题,const char*但许多用户std::string无论如何都会有一个。如果您不关心效率std::stringstd::string即使您有一个const char*.

It's simpler than std::strtollwhich will always require 3 arguments.

它比std::strtoll总是需要 3 个参数更简单。

It should throw if the input is not a number, but see these comments.

如果输入不是数字,它应该抛出,但请参阅这些评论

回答by R.. GitHub STOP HELPING ICE

Doing this 100% portably is a little bit tricky. long longis required to be at least 64 bits, but need not necessarily be twos complement, so it might not be able to represent -0x7fffffffffffffff-1, and thus using strtollcould have a broken corner case. The same issue applies to strtoimax. What you could do instead is consume leading space (if you want to allow leading space) and check for the sign first, then use strtoullor strtoumax, either of which is required to support values up to the full positive range of int64_t. You can then apply the sign:

100% 可移植地执行此操作有点棘手。long long要求至少为 64 位,但不一定是二进制补码,因此它可能无法表示-0x7fffffffffffffff-1,因此使用strtoll可能会出现坏角情况。同样的问题适用于strtoimax. 您可以做的是消耗前导空间(如果您想允许前导空间)并首先检查符号,然后使用strtoullor strtoumax,其中任何一个都需要支持高达int64_t. 然后,您可以应用该标志:

unsigned long long x = strtoull(s, 0, 0);
if (x > INT64_MAX || ...) goto error;
int64_t y = negative ? -(x-1)-1 : x;

This logic is written to avoid all overflow cases.

编写此逻辑是为了避免所有溢出情况。

回答by Kninnug

strtollconverts it to a long longwhich is usually a 64-bit int.

strtoll将其转换long long为通常是 64 位 int 的 a。

回答by Fritz Pom

This worked for me with a different int64 type, and I like the clean C++ style:

这对我使用了不同的 int64 类型,我喜欢干净的 C++ 风格:

std::istringstream iss(argv[i]);
int64_t i64;
iss >> i64;

You may get an compile error: operartor<<... is not defined.

您可能会收到编译错误:operartor<<... 未定义。

And I don't know what happens, if argv[i] contains "HALLO".

如果 argv[i] 包含“HALLO”,我不知道会发生什么。