如何从 const char* 转换为 unsigned int c++

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

How to convert from const char* to unsigned int c++

c++

提问by Bizwoo

I am new in c++ programming and I have been trying to convert from const char* to unsigned int with no luck. I have a:

我是 C++ 编程的新手,我一直在尝试从 const char* 转换为 unsigned int ,但没有运气。我有一个:

const char* charVar;

and i need to convert it to:

我需要将其转换为:

unsigned int uintVar;

How can it be done in C++?

如何在 C++ 中完成?

Thanks

谢谢

回答by Steve Townsend

#include <iostream>
#include <sstream>

const char* value = "1234567";
stringstream strValue;
strValue << value;

unsigned int intValue;
strValue >> intValue;

cout << value << endl;
cout << intValue << endl;

Output:

输出:

1234567

1234567

1234567

1234567

回答by ?imon Tóth

What do you mean by convert?

你说的转换是什么意思?

If you are talking about reading an integer from the text, then you have several options.

如果您正在谈论从文本中读取整数,那么您有多种选择。

Boost lexical cast: http://www.boost.org/doc/libs/1_44_0/libs/conversion/lexical_cast.htm

提升词法转换:http: //www.boost.org/doc/libs/1_44_0/libs/conversion/lexical_cast.htm

String stream:

字符串流:

const char* x = "10";
int y;
stringstream s(x);
s >> y;

Or good old C functions atoi()and strtol()

或者好的旧 C 函数atoi()strtol()

回答by ogni42

If you really want to convert a pointer to a constant character into an unsigned int then you should use in c++:

如果您真的想将指向常量字符的指针转换为无符号整数,那么您应该在 C++ 中使用:

const char* p;
unsigned int i = reinterpret_cast<unsigned int>( p );

This converts the address to which the pointer points to into an unsigned integer.

这会将指针指向的地址转换为无符号整数。

If you want to convert the content to which the pointer points to into an unsigned int you should use:

如果要将指针指向的内容转换为 unsigned int,则应使用:

const char* p;
unsigned int i = static_cast<unsigned int>( *p );

If you want to get an integer from a string, and hence interpret the const char* as a pointer to a const char array, you can use one of the solutions mentioned above.

如果您想从字符串中获取整数,从而将 const char* 解释为指向 const char 数组的指针,则可以使用上述解决方案之一。

回答by Eric Towers

The C way:

C方式:

#include <stdlib.h>
int main() {
    const char *charVar = "16";
    unsigned int uintVar = 0;

    uintVar = atoi(charVar);

    return 0;
}

The C++ way:

C++方式:

#include <sstream>
int main() {
    istringstream myStream("16");
    unsigned int uintVar = 0;

    myStream >> uintVar;

    return 0;
}

Notice that in neither case did I check the return code of the conversion to make sure it actually worked.

请注意,在这两种情况下,我都没有检查转换的返回码以确保它确实有效。

回答by Helmut Grohne

In C this can be done using atoiwhich is also available to C++ via cstdlib.

在 C 中,这可以使用atoi它来完成,C++ 也可以通过cstdlib.

回答by Sdra

I usually use this generic function to convert a string into "anything":

我通常使用这个通用函数将字符串转换为“任何东西”:

  #include <sstream>
  // Convert string values into type T results.
  // Returns false in case the conversion fails.
  template <typename T>
  bool getValueFromString( const std::string & value, T & result )
  {
    std::istringstream iss( value );
    return !( iss >> result ).fail();
  }

just use it as in:

只需将其用作:

int main()
{
  const char * a_string = "44";
  unsigned int an_int;
  bool         success;

  // convert from const char * into unsigned int
  success = getValueFromString( a_string, an_int );


  // or any other generic convertion
  double       a;
  int          b;
  float        c;
  // conver to into double
  success = getValueFromString( "45", a );

  // conve rto into int
  success = getValueFromString( "46", b );

  // conver to into float
  success = getValueFromString( "47.8", c );
}

回答by Armen Tsirunyan

atoifunction will convert const char* to int, which can be implicitly converted to unsigned. This won't work for large integers that don't fit in int.

atoi函数会将 const char* 转换为 int,后者可以隐式转换为 unsigned。这不适用于不适合 int 的大整数。

A more C++-ish way is to use strings and streams

更像 C++ 的方式是使用字符串和流

#include <sstream>
#include <string>

int main()
{
   std::string strVar;
   unsigned uintVar;
   std::istringstream in(strVar);
   in >> uintVar;
}

An easier but nonstandard way would be to use boost's lexical cast.

一种更简单但非标准的方法是使用 boost 的词法 cast

HTH

HTH

回答by Cercerilla

Without more information there is no way to properly answer this question. What are you trying to convert exactly? If charVar is an ascii representation of the string, then you can do like others have suggested and use stringstreams, atoi, sscanf, etc.

如果没有更多信息,就无法正确回答这个问题。你到底想转换什么?如果 charVar 是字符串的 ascii 表示,那么您可以像其他人建议的那样使用 stringstreams、atoi、sscanf 等。

If you want the actual value pointed to by charVar, then instead you'd want something like:

如果您想要 charVar 指向的实际值,那么您需要类似的东西:

intValue = (unsigned int)(*charVal);

Or if charVal is the pointer to the first byte of an unsigned integer then:

或者,如果 charVal 是指向无符号整数的第一个字节的指针,则:

intValue = *((unsigned int*)(charVal));

回答by mcvz

const char* charVar = "12345";
unsigned int uintVar;
try {
  uintVar = std::stoi( std::string(charVar) );
}
catch(const std::invalid_argument& e) {
  std::cout << "Invalid Arg: " << e.what() << endl;
}
catch(const std::out_of_range& e) {
  std::cout << "Out of range: " << e.what() << endl;
}

回答by Vishwanath Kamath

You can also use strtoulor _tcstoulto get unsigned long value from const char*and then cast the value to unsigned int.

您还可以使用strtoul_tcstoul从中获取 unsigned long 值const char*,然后将该值转换为 unsigned int。

http://msdn.microsoft.com/en-us/library/5k9xb7x1(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/5k9xb7x1(v=vs.71).aspx