Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

atof with base information

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
0
0
US
I'm trying to convert strings to base 10 integers... but I'm not starting in base 10, so what I'm looking for would basically be an atof function where I can also supply a base... anyone know if this is possible with built in functions, or am I writing this?
 
It depends on what base the string is in. If it is in binary, use bitset. If it is in hexadecimal or octal, use a stringstream. Here's some examples:
Code:
#include <iostream>
#include <string>
#include <bitset>
#include <sstream>

int main()
{
    int value = 0;

    std::string binary = "11110001001000000";
    std::bitset<sizeof(int)*8> bs(binary);
    value = bs.to_ulong();
    std::cout << binary << " converts to " << value << '\n';

    std::string octal = "0361100"; // Leading 0 not necessary.
    std::istringstream iso(octal);
    iso >> std::oct >> value;
    std::cout << octal << " converts to " << value << '\n';

    std::string hexadecimal = "0x1E240"; // Leading 0x not necessary.
    std::istringstream ish(hexadecimal);
    ish >> std::hex >> value;
    std::cout << hexadecimal << " converts to " << value << '\n';
}
 
it's just an arbitrary base between 16-35. I've already written a simple function to convert it, but I figured if there's already a nice built in method to do so I should probably be using that.
 
Ok. I Personally don't know of any existing functions that would do that.

Good luck with your own. You could always post it here for tips and comments.
 
skiflyer said:
I've already written a simple function to convert it
I did that a few years ago too (just for fun). Once you look at the source code for the atoi() function, it's easy to specify just about any base. ;-)
 
Lifted from a codeproject article (I need to tweak it a bit yet to updated the deprecated functions)

Code:
int StrToNum(const TCHAR *udata, int udatalen, int base)
{
	long index;
	const TCHAR numdigits[] = 
		TEXT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	long digitValue = 0;
	long RetVal = 0;
	TCHAR digits[sizeof(numdigits)+1];
	TCHAR *dataVal;
	TCHAR data[512];

	_tcscpy(data, udata);
	_tcsupr(data);

	ZeroMemory(digits, sizeof(digits));

	_tcsncpy(digits, numdigits, base);

	for(index = 0; index < udatalen; index++)
	{
		dataVal = _tcschr(digits, data[index]);
		if (dataVal != 0)
		{
			digitValue = long(dataVal - digits);
			RetVal = RetVal * base + digitValue;
		}
	}
	return RetVal;
}
 
Remember strtol from <cstdlib>:
Code:
long strtol( const char *nptr, char **endptr, int base );
The base arg is between 2 and 36 (or a special case 0, see strtol description).
 
Other than doing it out of bordom, is there any reason why anyone would ever need to use a base other than 2, 8, 10 or 16? Base 36 seems a little ridiculous...
 
I use a rolling base on my registration keys just to shuffle them up a bit and to guarantee me two digits for each ASCII character. (16-35 base will give you said guarantee)... plus that way I can tweak it a bit to never have an O(letter oh)... by stopping at base 35 and swapping the O's for Z's in my strings so they won't be confused with the zeros.

 
I've never had the need to use a non-standard base myself, but I guess it makes sense for something like that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top