CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Anyone knows how to do this



    typedef std::string T_String;
    typedef char T_8bit;
    typedef short T_16bit;
    typedef int T_32bit;

    I have defined these typedefs and I want to convert the "T_string" type to short,char and int type.

    I have a function like this:

    void C_Ptx::Built_AMB(T_String value,bool ChainType)

    where I need to convert the 1st parameter to short,char and int type.

    How can I do it! please Help.

    Kohinoor

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Anyone knows how to do this

    two options spring to mind:

    For a char it is simple - the first character is your 'char'

    For the others:

    1. Use a C function such as strtol, but you'll need to call c_str() on the string. (No doubt the method Thew would suggest)

    2. Create an ostringstream from the string and then use >>

    You can do the same with double and also if you have wstring, in which case you use owstringstream instead.


    The best things come to those who rate

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Anyone knows how to do this

    What do you mean by "convert"? If you mean you want to read a value coded in the string, then that's relatively simple:

    #include <string>
    #include <sstream>
    // read an int from a string
    std::string s = "12345";
    std::istringstream iss(s);
    int i;
    iss >> i; // i now equals 12345



    If, however, by "convert" you mean "cast", then forget it.

    He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Anyone knows how to do this

    "istringstream", surely :-)

    He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Anyone knows how to do this

    yes of course



    The best things come to those who rate

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured