Welcome to AC Web.
Results 1 to 12 of 12

Thread: Int to string?

  1. #1

    Int to string?


    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    Code:
    int myint = 2;
    const char *mystring = "Hello I got this number";
    How do I get 'myint' into 'mystring'?

    I tried:
    Code:
    > const char *mystring = "Hello I got this number %d", myint;
    > const char *mystring = "Hello I got this number %s", std::to_string(myint).c_str();
    > const char *mystring = "Hello I got this number %s", std::to_string(myint);
    None of these working.

    EDIT: Also cannot use '+' between string, the fuck? (Missing #include????)
    Last edited by molinita; 12-12-2019 at 03:26 PM.

  2. #2


    Join Date
    Nov 2015
    Location
    Library of Elchea
    Posts
    682
    You could use ostringstream (documentation) for this.

    Example:
    Code:
    std::ostringstream oss;
    oss << "Hello I got this number " << yourNumber;
    std::string yourString = oss.str();

  3. #3
    Quote Originally Posted by Loud21 View Post
    You could use ostringstream (documentation) for this.

    Example:
    Code:
    std::ostringstream oss;
    oss << "Hello I got this number " << yourNumber;
    std::string yourString = oss.str();
    Is not working for me,
    I need to send the string in this function:

    Code:
    player->GetSession()->SendNotification(mystring);
    And this way is not working.

  4. #4


    Join Date
    Nov 2015
    Location
    Library of Elchea
    Posts
    682
    Try this
    Code:
    player->GetSession()->SendNotification(mystring.c_str());

  5. #5
    Maybe
    Code:
    std::string myString = "Hello I got this number ";
    myString += std::to_string(myint);
    
    player->GetSession()->SendNotification(myString);

  6. #6
    Yo now it worked!
    Thank you +rep

    - - - Updated - - -

    Quote Originally Posted by Alistar View Post
    Maybe
    Code:
    std::string myString = "Hello I got this number ";
    myString += std::to_string(myint);
    
    player->GetSession()->SendNotification(myString);
    Won't let me '+' between strings

  7. #7
    Quote Originally Posted by molinita View Post
    Yo now it worked!
    Thank you +rep

    - - - Updated - - -



    Won't let me '+' between strings
    Can you show us the full code?


    Last edited by Alistar; 12-12-2019 at 04:34 PM.

  8. #8
    #include <string>


    player->GetSession()->SendNotification(myString); - this might work on windows but not linux, SendNotification expects const char* , so you can convert myString to const char* by myString.c_str()

    so

    player->GetSession()->SendNotification(myString.c_str());

  9. #9
    Getting an int to a string literally one google search away

  10. #10
    Derp of the herp dept.

    Join Date
    Oct 2011
    Location
    Finland
    Posts
    726
    boost::format fmt = boost::format("Hello I got this number %1") % myint;

  11. #11
    Quote Originally Posted by Hamar View Post
    boost::format fmt = boost::format("Hello I got this number %1") % myint;
    Using boost to just format a string is like using an industrial kitchen to cook cup noodles.

  12. #12
    weird like that

    Join Date
    Feb 2010
    Location
    http://rochet2.github.io/
    Posts
    5,280

    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    In most cases you must convert std::string to const char* like Ckabot said.

    All of the examples below should compile and they should output "Hello I got this number 5".

    In TC and other cores you can usually provide a format and extra parameters to the function, especially the ones that start with P, such as PQuery. The functions work just like printf does, see http://www.cplusplus.com/reference/cstdio/printf/. Note that you must use different format characters for different types, such as %i %u %s %f. You should probably prefer this choice unless you have some reason not to.
    Code:
    player->GetSession()->SendNotification("Hello I got this number %i", 5);
    You can also use the format library that TC has. Other cores may not include fmt library though and it is not on older core versions.
    Code:
    #include "StringFormat.h"
    #include <string>
    std::string output = Trinity::StringFormat("Hello I got this number %i", 5);
    player->GetSession()->SendNotification(output.c_str());
    There is also the good old classic that uses a buffer.
    Code:
    #include <cstdio>
    char buf[100];
    snprintf(buf, 100, "Hello I got this number %i", 5);
    player->GetSession()->SendNotification(buf);
    Alistar's code should work.
    Code:
    #include <string>
    std::string myString = "Hello I got this number ";
    myString += std::to_string(5);
    player->GetSession()->SendNotification(myString.c_str());
    Loud21's stringstream version should work fine.
    Code:
    #include <sstream>
    #include <string>
    std::ostringstream oss;
    oss << "Hello I got this number " << 5;
    std::string yourString = oss.str();
    player->GetSession()->SendNotification(yourString.c_str());
    Hamar's boost code should work, given that you have boost format.
    Code:
    #include <boost/format.hpp>
    boost::format fmt = boost::format("Hello I got this number %1") % 5;
    player->GetSession()->SendNotification(fmt.str().c_str());
    Last edited by Rochet2; 12-15-2019 at 06:20 PM.

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •