All pastes #756716 Raw Edit

numeral system convert part 1

public cpp v1 · immutable
#756716 ·published 2007-10-31 18:17 UTC
rendered paste body
/*  	Author: Alex A. Smirnov	This program is free software; you can redistribute it and/or	modify it under the terms of the GNU General Public License as	published by the Free Software Foundation; either version 2, or (at	your option) any later version.	This program is distributed in the hope that it will be useful, but	WITHOUT ANY WARRANTY; without even the implied warranty of	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU	General Public License for more details.	You should have received a copy of the GNU General Public License	along with this program; if not, write the Free Software	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA	02110-1301 USA*/#include <iostream>#include <algorithm>#include <sstream>#include <string>namespace Convert{	namespace Utils{		int StringToInt(const std::string &stringValue)		{			int iReturn;			std::stringstream ssStream(stringValue);			ssStream >> iReturn;			return iReturn;		}		std::string IntToString(int iValue) 		{ 			std::stringstream ssStream;			ssStream << iValue;			return ssStream.str();		}		unsigned short digitToInt(char number)		{			if (number >= '0' && number <= '9')				return number - '0';			if (number >= 'A' && number <= 'Z')				return number - 'A' + 10;			return 0;		}		char intToDigit(unsigned short number)		{			if (number >= 0 && number <= 9)				return '0' + number;			if (number >= 10 && number <= 35)				return 'A' + number - 10;			return 0;		}	}	std::string decimalToAny(double number, unsigned short radix, int accuracy=5)	{		std::string result;		int wholep = int(number);		double fracp = number - wholep;		for (; wholep != 0; wholep /= radix)			result.push_back(Utils::intToDigit(wholep % radix));		std::reverse(result.begin(), result.end());        		if (fracp) {			result.push_back('.');			do {				fracp *= radix;				wholep = int(fracp);				fracp -= wholep;				result.push_back( Utils::intToDigit(wholep) );			} while (--accuracy && fracp);		}		return result;	}}