C++ 将字符串转换为 uint64_t

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42356939/
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-08-28 15:29:28  来源:igfitidea点击:

C++ convert string to uint64_t

c++stringuint64

提问by Cauchy

I am trying to convert from a string to a uint64_t integer. stoireturns a 32 bit integer, so it won't work in my case. Are there other solutions?

我正在尝试从字符串转换为 uint64_t 整数。stoi返回一个 32 位整数,所以它在我的情况下不起作用。还有其他解决方案吗?

采纳答案by π?ντα ?ε?

Did you try

你试过了吗

uint64_t value;
std::istringstream iss("18446744073709551610");
iss >> value;

?

?

See Live Demo

观看现场演示



That may work for out of date standards too.

这也可能适用于过时的标准。

回答by Gambit

Try std::stoullif you are using C++11 or greater.

std::stoull如果您使用的是 C++11 或更高版本,请尝试。

This postmay also be of help. I didnt mark this as a duplicate because the other question is about C.

这篇文章也可能有帮助。我没有将其标记为重复,因为另一个问题是关于 C。

回答by Vada Poché

If you're using boost, you could make use of boost::lexical_cast

如果您使用的是 boost,则可以使用boost::lexical_cast

#include <iostream>
#include <string>
#include <boost-1_61/boost/lexical_cast.hpp> //I've multiple versions of boost installed, so this path may be different for you

int main()
{
    using boost::lexical_cast;
    using namespace std;

    const string s("2424242");
    uint64_t num = lexical_cast<uint64_t>(s);
    cout << num << endl;

    return 0;
}

Live example: http://coliru.stacked-crooked.com/a/c593cee68dba0d72

现场示例:http: //coliru.stacked-crooked.com/a/c593cee68dba0d72

回答by Gimhani

You can use strtoull() from <cstdlib> if you are using C++11 or newer. Else if you need this with c99 as well, you can go for strtoull() function in stdlib.hfrom C.

如果您使用的是 C++11 或更新版本,则可以使用 < cstdlib> 中的strtoull() 。否则,如果您也需要使用 c99,您可以从 C 的stdlib.h 中使用 strtoull() 函数。

See the following example

看下面的例子

#include <iostream>
#include <string>
#include <cstdlib> 

int main()
{
  std::string value= "14443434343434343434";
  uint64_t a;
  char* end;
  a= strtoull( value.c_str(), &end,10 );
  std::cout << "UInt64: " << a << "\n";
}

回答by Hiloliddin Jaloliddinzoda

NOTE: This is solution for c not for C++. So it maybe harder then in C++. Here we convert String HEX to uint64_t hex value. All individual characters of string is converted to hex integer ony by one. For example in base 10 -> String = "123":

注意:这是 c 的解决方案,而不是 C++。所以在 C++ 中可能更难。这里我们将 String HEX 转换为 uint64_t 十六进制值。字符串的所有单个字符都被转换为一个十六进制整数。例如在基数 10 -> String = "123" 中:

  • 1st loop : value is 1
  • 2nd loop : value is 1*10 + 2 = 12
  • 3rd loop : value is 12*10 + 3 = 123
  • 第一个循环:值为 1
  • 第二个循环:值为 1*10 + 2 = 12
  • 第三个循环:值为 12*10 + 3 = 123

So like this logic is used to convert String of HEX character to uint_64hex value.

所以就像这个逻辑用于将 HEX 字符的 String 转换为 uint_64hex 值。

uint64_t stringToUint_64(String value) {
  int stringLenght = value.length();

  uint64_t uint64Value = 0x0;
  for(int i = 0; i<=stringLenght-1; i++) {
    char charValue = value.charAt(i);

    uint64Value = 0x10 * uint64Value;
    uint64Value += stringToHexInt(charValue);
  }

  return uint64Value;
}

int stringToHexInt(char value) {
  switch(value) {
    case '0':
      return 0;
      break;
    case '1':
      return 0x1;
      break;
    case '2':
      return 0x2;
      break;
    case '3':
      return 0x3;
      break;
    case '4':
      return 0x4;
      break;
    case '5':
      return 0x5;
      break;
    case '6':
      return 0x6;
      break;
    case '7':
      return 0x7;
      break;
    case '8':
      return 0x8;
      break;
    case '9':
      return 0x9;
      break;
    case 'A':
    case 'a':
      return 0xA;
      break;
    case 'B':
    case 'b':
      return 0xB;
      break;
    case 'C':
    case 'c':
      return 0xC;
      break;
    case 'D':
    case 'd':
      return 0xD;
      break;
    case 'E':
    case 'e':
      return 0xE;
      break;
    case 'F':
    case 'f':
      return 0xF;
      break;
  }
}