Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to short in C++

Tags:

c++

string

short

So I've looked around for how to convert a string to a short and found a lot on how to convert a string to an integer. I would leave a question as a comment on those threads, but I don't have enough reputation. So, what I want to do is convert a string to a short, because the number should never go above three or below zero and shorts save memory (as far as I'm aware).

To be clear, I'm not referring to ASCII codes.

Another thing I want to be able to do is to check if the conversion of the string to the short fails, because I'll be using a string which consists of a users input.

I know I can do this with a while loop, but if there's a built in function to do this in C++ that would be just as, or more, efficient than a while loop, I would love to hear about it.

like image 257
Logan Kling Avatar asked Feb 13 '15 03:02

Logan Kling


4 Answers

You can also use ssprintf with the %hi format specifier.

Example:

short port;
char szPort[] = "80";

sscanf(szPort, "%hi", &port);
like image 120
John Doe Avatar answered Oct 16 '22 15:10

John Doe


the number should never go above three or below zero

If you really really need to save memory, then this will also fit in a char (regardless whether char is signed or unsigned).

Another 'extreme' trick: if you can trust there are no weird things like "002" then what you have is a single character string. If that is the case, and you really really need performance, try:

char result = (char)( *ptr_c_string - '0' );
like image 27
Bert Bril Avatar answered Sep 19 '22 18:09

Bert Bril


Basically, an std::stos function is missing for unknown reasons, but you can easily roll your own. Use std::stoi to convert to int, check value against short boundaries given by e.g. std::numeric_limits<short>, throw std::range_error if it's not in range, otherwise return that value. There.

If you already have the Boost library installed you might use boost::lexical_cast for convenience, but otherwise I would avoid it (mainly for the verbosity and library dependency, and it's also a little inefficient).

Earlier boost::lexical_cast was known for not being very efficient, I believe because it was based internally on stringstreams, but as reported in comments here the modern version is faster than conversion via stringstream, and for that matter than via scanf.

like image 13
Cheers and hth. - Alf Avatar answered Oct 16 '22 16:10

Cheers and hth. - Alf


An efficient way is to use boost::lexical_cast:

short myShort = boost::lexical_cast<short>(myString);

You will need to install boost library and the following include: #include <boost/lexical_cast.hpp>

You should catch bad_lexical_cast in case the cast fails:

    try
    {
        short myShort = boost::lexical_cast<short>(myString);
    }
    catch(bad_lexical_cast &)
    {
        // Do something
    }
like image 5
Jérôme Avatar answered Oct 16 '22 15:10

Jérôme