简体   繁体   English

C ++不使用C标准库将字符串和整数转换为char *

[英]c++ convert string and int to char* not using the C standard library

not using the C standard library do this in C++ ? 不使用C标准库在C ++中执行此操作?

  1. Convert string to char* 将字符串转换为char *
  2. Covert int to char* 从int转换为char *

If i had to convert string to int in c++ use something like this 如果我必须在C ++中将字符串转换为int,请使用类似以下的内容

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
    int num;
    string str="2020";
    (stringstream)"2020">>num;
    cout<<num+2;
    return 0;
}

"Convert string to char * " is impossible without the C++ standard library since string is a part of that library. 没有C ++标准库,“将string转换为char * ”是不可能的,因为string是该库的一部分。

"Convert int to char * ": I assume you mean putting the decimal representation of an int in some buffer. “将int转换为char * ”:我假设您的意思是将int的十进制表示形式放入某些缓冲区中。 This is how it can be done for unsigned ; 这就是如何对unsigned doing the same for signed int means you have to take a possible - into account, and the corner case that arises from the fact that -INT_MIN is not well-defined. 对带signed int进行相同的处理意味着您必须考虑-可能的情况,并且-INT_MIN的定义不充分会导致出现-INT_MIN情况。

unsigned n = SOME_VALUE;

char buffer[11]; // long enough for 32-bit UINT_MAX + NUL character
char *p = buffer + sizeof(buffer);

*--p = '\0';
do {
    *--p = '0' + n % 10;
    n /= 10;
} while (n);

p now points to the string representation of n . p现在指向n的字符串表示形式。

I assume you mean you want a C++ solution using the C++ standard library (not using the C standard library). 我假设您的意思是您想要使用C ++标准库(而不使用C标准库)的C ++解决方案。 If so try the code below: 如果是这样,请尝试以下代码:

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    string stringString("2020");
    cout << "String String = " << stringString << endl;

    const char* charString = stringString.c_str();
    cout << "Char String = " << charString << endl;

    int charStringLen = stringString.size();
    for (int characterIndexCtr = 0; characterIndexCtr < charStringLen; ++characterIndexCtr)
    {
        cout << "Character At Index " << characterIndexCtr << " = " << charString[characterIndexCtr] << endl;
    }

    stringstream stringStream(stringString);
    int integerNumber;
    stringStream >> integerNumber;
    cout << "Integer = " << integerNumber << endl;
    cout << "Integer + 2 = " << integerNumber + 2 << endl;

    cout << "Press Enter To End Program ... ";
    cin.get();

    return 0;
}

I don't know whether you count Boost as stdlib, but lexical_cast can cast char*s to whatever you want: 我不知道您是否将Boost视为stdlib,但是lexical_cast可以将char * s转换为您想要的任何内容:

char* foo = "123"
int bar = boost::lexical_cast(foo);

And the other way round: 反过来:

int foo = 123;
std::string bar = boost::lexical_cast(foo);
your_function(bar.c_str());

It's using stringstream behind the scenes, but is a lot easier to use. 它在后台使用stringstream,但使用起来容易得多。
Also, you can't just convert an int to char*, as the memory the char* is pointing to has to be allocated somewhere. 同样,您不能只将int转换为char *,因为char *指向的内存必须分配到某个地方。

#include <sstream>
#include <iostream>
#include <string>

int main (int argc, char* argv[])
{
    std::string str ("123");
    const char* c_str = str.c_str();

    char* so_bad = const_cast<char*>(c_str);

    std::stringstream ss;
    ss << so_bad;

    int int_value;
    ss >> int_value;

    std::cout << int_value;

    return 0;
}    

Convert string to char * string转换为char *

std::vector<char> vec( str.begin(), str.end() );
vec.push_back( '\0' );
char * data = &vec[0];

Convert string to int string转换为int

std::istringstream iss(str);
int i;
if( !iss >> i )
{
    std::ostringstream oss;
    oss << "Invalid conversion from " << str << " to integer";
    throw std::invalid_argument( oss.str() );
}

Your second answer was close to the way to do it. 您的第二个答案接近完成该任务的方式。 Note there is a boost::lexical_cast which does pretty much the same, but has the huge downside of a meaningless bad_cast exception that gives no context information and therefore renders it almost useless in my opinion. 请注意,有一个boost :: lexical_cast几乎一样,但是有一个毫无意义的bad_cast异常的巨大缺点,该异常不提供上下文信息,因此在我看来几乎没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM