Sign in to follow this  
gretty

How do you convert between Primitive Data Types?

Recommended Posts

Hello

 

How do you convert between primitive data types (limited only to STL) and why do you use that methodology/methodologies?

 

In my work environment we stick strictly to STL and typically use StringStream to convert between primitive data types. I am looking to hear how you do it and also for advice on more efficient/better methodologies to perform this conversion as I've heard StringStream can be slow? I am going to get into Embedded C++ so these expensive conversions will really start to matter.

 

I'm aware of atoi, atof, etc. but these only work for c strings apparently. Thanks in advance for your advice and feedback.

Edited by gretty

Share this post


Link to post
Share on other sites

STL typically either meant HP's libraries or SGI's libraries primarily used in-house.


Ok, I agree with the terminology sentiment for the most part, but this is off base. "STL" is still a very commonly used term (even by various members of the C++ committee and by compiler and stdlib implementors).

Microsoft calls it the STL
cpluplus.com still calls it the STL
Scott Meyers has a whole series of old but popular books with STL in the title

STL is not the _official_ term, but it's perfectly obvious what it refers to.

Again, the OP was rather unclear, but it wasn't the term "STL" that made it so.

Share this post


Link to post
Share on other sites
You mentioned embedded programming. A lot of embedded systems work with very old or very limited C++ environments, so I'll go over both the modern shiny new ways of doing this, the older ways that your system supports, and the low-level hacky embedded programmer ways they might be done.
 

In my work environment we stick strictly to STL and typically use StringStream to convert between primitive data types. I am looking to hear how you do it and also for advice on more efficient/better methodologies to perform this conversion as I've heard StringStream can be slow?

I'm aware of atoi, atof, etc. but these only work for c strings apparently. Thanks in advance for your advice and feedback.


Modern C++ provides in the <string> header several sets of functions for converting between primitive numeric types and std::string:

std::to_string is an overloaded function that converts a value into a std::string, unsurprisingly.
std::stoi - and its cohorts stol, stod, stof, etc. - do the reverse and convert a std::string into a primitive type.

See http://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c for a more thorough explanation and more links.

These facilities can be problematic for embedded developers or game developers, though. First, they're only supported on newer C++ implementations. Second, they all require marshalling your strings through std::string which in turns require copies and allocations if you're moving the data back and forth between network buffers or the like. Third, they rely on exceptions as their error mechanism.

The older methods for C++ are exactly what you describe. The performance isn't great, but it's not "bad" unless you are measuring an actual problem. If you're only formatting a handful of strings for debug purposes every once in a while, who cares if the C++ IOStreams stuff is slow? If you're unsure if there might be a problem down the road, encapsulate all of your string formatting/conversion behind some functions and then you can freely optimize the code without requiring any rewrites.

To avoid all of these issues you can use the C library equivalents. The above will often just wrap around the C versions anyway, so the C versions will generally be as fast or faster just by that fact alone.

You can easily use C++ strings with C libraries in most cases. You can grab a C string from a std::string using the .c_str() method, and you can do the same for a string_buf (stringstream) as well.

Simply writing your own conversions for integral types (bool, int, long, signed and unsigned, etc.) is quite easy, though it should be your absolute last resort. Writing your own conversion routine for floating-point types is about one step short of impossible that nobody other than a real computational scientist with years of IEEE754 study should even attempt; thankfully, there's free code online written by such masters, should you need it. In general, I'd recommend sticking to the C++ facilities first and the C facilities after that and only doing it manually if you _really_ need something special.

Some embedded systems will supply their own very high performance conversion routines. There's even a handful that have direct hardware support and intrinsics for such conversions, but it's rather unlikely you're working with any of those (I don't know of any in mass production). Even without hardware support, a chip vendor may have some hardware considerations that led to them writing their own non-standard libraries. Check your embedded platform's documentation.

While you said you want to stick to the STL, do keep in mind that there are other formatting libraries out there. cppformat is great for generating formatted strings and is quite speedy and efficient, plus it includes helper routines for the simpler primitive type conversions. Boost has a lexical_cast<> library, though it's mostly superseded by the C++11 standard library additions (and Boost is almost a dirty word in many circles, especially in embedded and game programming).

Share this post


Link to post
Share on other sites

We may be thoroughly off-topic by this point, but I don't feel that it is unreasonable to advocate the use of accurate terminology on a technical subforum.

 

Microsoft calls it the STL

Microsoft only refers to the subset dealing specifically with containers and iterators as the STL. If you go one level up in their navigation, you will note that they correctly refer to the entirely as the 'C++ Standard Library'.
 

cpluplus.com still calls it the STL

While the URL does in fact still contain the text STL, I challenge you to find more than 3 mentions of it in the text of the site.
 

Scott Meyers has a whole series of old but popular books with STL in the title

Published in 2001, which IIRC, is before the term 'STL' fell out of use.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this