string to s32

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
ArakisTheKitsune
Posts: 73
Joined: Sat Jun 27, 2009 6:52 am

string to s32

Post by ArakisTheKitsune »

It's not much but I am kinda surprised that this function does not exists. I needed it so I wrote one.
Just add this code in irrString.h

Code: Select all

s32 toS32(void)
        {
                s32 result = 0;
 
                stringw buff = this->subString(0, this->findFirstChar(L"."));
                if(buff == "")
                {
                        buff = this->c_str();
                }
 
                for(u32 i = 0; i < buff.size(); ++i)
                {
                        
                        s32 y = 0;
 
                        if( (s32(buff[i]) < 48) || (s32(buff[i]) > 57) )
                        {
                                return 0;
                        }
 
                        if(buff.size() == 1)
                        {
                                return (s32(buff[i]) - 48);
                        }
                        else
                        {
                                y = (s32(buff[i]) - 48);
                                if( i < (buff.size() - 1))
                                {
                                        s32 m = 1;
                                        for(u32 z = 1; z < (buff.size() - i); ++z )
                                        {
 
                                                m *= 10;
                                        }
 
                                        y *= m;
                                }
                        }
 
                        result += y;
                        
                }
 
                return result;
        }
If code find any non number char it will return 0.
If number is float or double, numbers after the decimal point will be trimmed.
Knowledge is power, understanding is wisdom.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: string to s32

Post by REDDemon »

isn't there a floating point parser in the XML reader?
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: string to s32

Post by CuteAlien »

No, the stuff is in irr::core namespace in the fast_atof.h header. Corresponding function in Irrlicht would be irr::core::strtol10.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
ArakisTheKitsune
Posts: 73
Joined: Sat Jun 27, 2009 6:52 am

Re: string to s32

Post by ArakisTheKitsune »

Yes... I was not aware of that.
This is much simpler:

Code: Select all

s32 toS32(void)
        {
                core::stringc c(this->c_str());
                return core::strtol10(c.c_str());
        }
I am kinda embarrassment but hey every day you learn something new.
Knowledge is power, understanding is wisdom.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: string to s32

Post by CuteAlien »

Don't worry, I think I found those functions also only after using Irrlicht for a long time :-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Voxel
Posts: 8
Joined: Wed Feb 23, 2011 9:04 am

Re: string to s32

Post by Voxel »

Hi everyone,

I would like to share the following small code snippet with you. You can convert a value of any type into another result type. The only dependency is that output and input oterator have to exist for the types. Oh, and it's STL based...
The syntax corresponds to the cast operators.

Code: Select all

#ifndef __CONVERT_H__
#define __CONVERT_H__
 
#include <sstream>
 
template <class result_type, class value_type>
inline result_type& convert(value_type value, result_type& result)
 {
    std::stringstream stream;
    stream << value;      // insert value to stream
    stream >> result;     // write value to result
    return result;
}
 
template <class result_type, class value_type>
inline result_type convert(value_type value)
 {
    result_type result; // store conversions result here
    return convert<result_type, value_type>(value, result);
}
 
#endif
e.g. converting floating point value into string and vice versa using the second method.

Code: Select all

double fValue = convert<double>("42.0");
std::string sValue = convert<std::string>(fValue);
If you want to avoid the temporary value within the second method you can use the first method directly:

Code: Select all

std::string sValue2;
convert<double, std::string>(fValue, sValue);
or without the explicit template specification at the expense of readability:

Code: Select all

std::string sValue2;
convert(fValue, sValue2);
Maybe someone finds it useful...

Voxel
Post Reply