how to convert from int to char*?

前端 未结 10 2041
旧巷少年郎
旧巷少年郎 2020-11-30 18:37

The only way I know is:

#include 
#include 
using namespace std;

int main() {
  int number=33;
  stringstream strs;
  strs &l         


        
相关标签:
10条回答
  • 2020-11-30 19:12

    See this answer https://stackoverflow.com/a/23010605/2760919

    For your case, just change the type in snprintf from long ("%ld") to int ("%n").

    0 讨论(0)
  • 2020-11-30 19:15

    Alright.. firstly I needed something that did what this question is asking, but I needed it FAST! Unfortunately the "better" way is nearly 600 lines of code!!! Pardon the name of it that doesn't have anything to do with what it's doing. Proper name was Integer64ToCharArray(int64_t value);

    https://github.com/JeremyDX/All-Language-Testing-Code/blob/master/C%2B%2B%20Examples/IntegerToCharArrayTesting.cpp

    Feel free to try cleaning that code up without hindering performance.

    Input: Any signed 64 bit value from min to max range.

    Example:

    std::cout << "Test: " << AddDynamicallyToBuffer(LLONG_MAX) << '\n';
    std::cout << "Test: " << AddDynamicallyToBuffer(LLONG_MIN) << '\n';
    

    Output:

    Test: 9223372036854775807
    Test: -9223372036854775808
    

    Original Speed Tests: (Integer64ToCharArray();)

    Best case 1 digit value.

    Loops: 100,000,000, Time Spent: 1,381(Milli), Time Per Loop 13(Nano)

    Worse Case 20 Digit Value.

    Loops: 100,000,000, Time Spent: 22,656(Milli), Time Per Loop 226(Nano

    New Design Speed Tests: (AddDynamicallyToBuffer();)

    Best case 1 digit value.

    Loops: 100,000,000, Time Spent: 427(Milli), Time Per Loop 4(Nano)

    32 Bit Worst Case - 11 digit Value.

    Loops: 100,000,000, Time Spent: 1,991(Milli), Time Per Loop 19(Nano)

    Negative 1 Trillion Worst Case - 14 digit Value.

    Loops: 100,000,000, Time Spent: 5,681(Milli), Time Per Loop 56(Nano)

    64 Bit Worse Case - 20 Digit Value.

    Loops: 100,000,000, Time Spent: 13,148(Milli), Time Per Loop 131(Nano)

    How It Works!

    We Perform a Divide and Conquer technique and once we now the maximum length of the string we simply set each character value individually. As shown in above speed tests the larger lengths get big performance penalties, but it's still far faster then the original loop method and no code has actually changed between the two methods other then looping is no longer in use.

    In my usage hence the name I return the offset instead and I don't edit a buffer of char arrays rather I begin updating vertex data and the function has an additional parameter for offset so it's not initialized to -1.

    0 讨论(0)
  • 2020-11-30 19:17

    You can use boost

    #include <boost/lexical_cast.hpp>
    string s = boost::lexical_cast<string>( number );
    
    0 讨论(0)
  • 2020-11-30 19:17

    Converting our integer value to std::string so we can know how long (how long number of digits).

    Then we creating char array length of string letter size +1, so we can copy our value to string then char array.

    #include <string>
    
    char* intToStr(int data) {
        std::string strData = std::to_string(data);
    
        char* temp = new char[strData.length() + 1];
        strcpy(temp, strData.c_str());
    
       return temp;
    }
    
    0 讨论(0)
提交回复
热议问题