How to convert string to int64_t?

戏子无情 提交于 2019-11-30 08:15:37

A C99 conforming attempt.

[edit] employed @R. correction

// 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;
}

There are a few ways to do it:

  strtoll(str, NULL, 10);

This is POSIX C99 compliant.

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

 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>

strtoll converts it to a long long which is usually a 64-bit int.

Doing this 100% portably is a little bit tricky. long long is 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 strtoll could 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 strtoull or strtoumax, either of which is required to support values up to the full positive range of int64_t. You can then apply the sign:

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.

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

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

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

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

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

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

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!