Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T>
- std::basic_string<T> itos(int _Val, const unsigned _Radix=10)
- {
- int lg;
- if (_Radix<2 && _Radix>36)
- throw std::invalid_argument("The radix is out of bound.");
- if (_Val>0)
- lg=0;
- else if (_Val<0)
- lg=1, _Val=-_Val;
- else
- return std::basic_string<T>()+=T('0');//What the...
- for (int temp=_Val; temp; temp/=_Radix)
- ++lg;
- //std::use_facet<std::ctype<T> >(std::locale()).widen
- std::basic_string<T> Result(lg, T('0'));
- typename std::basic_string<T>::reverse_iterator iter=Result.rbegin();
- for (; _Val; ++iter, _Val/=_Radix)
- {
- int temp=_Val%_Radix;
- if (temp<10)
- *iter=temp+T('0');
- else
- *iter=temp+(T('A')-10);
- }
- if (iter!=Result.rend())
- *iter=T('-');
- return Result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement