Click here to Skip to main content
13,197,548 members (51,481 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 5 Feb 2002

Converting between different number bases

, 5 Feb 2002
Rate this:
Please Sign up or sign in to vote.
An article on converting numbers and strings to different code bases such as binary, octal, decimal and hex.
<!-- Add the rest of your HTML here -->


Recently I was working on a project to store binary data to a text file and read it again. The solution I thought to be simple and fast was to convert each byte into hexadecimal format and then again convert it to binary format. There are other methods such as Base64 encoding to do it but I was short of time. So I created two functions fully UNICODE compatible.

The following functions convert to any number basis such as binary, octal, decimal, hex or in range of 2 to 36.

int StrToNum(const TCHAR *udata, int datalen, int base) :  Convert any string to relevant numerical  base.

TCHAR* __fastcall NumToStr(TCHAR
*RetData, long number, int base)
  : Converts any number to string in the desired base.

Here is the complete code with examples

#include <windows.h>
#include <iostream.h>
#include <tchar.h>
//convert the string to number
//udata is string
//udatalen is length of string
//base is numerical base eg. for decimal it is 10
// for hex it is 16
//largest base supported here is upto 36

int __fastcall 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] ;
	//copy the data to our variable
	_tcscpy(data, udata);
	//convert it to upper case
	ZeroMemory(digits, sizeof(digits));
	//copy the number of digits supported by base in digits
	_tcsncpy(digits, numdigits, base);
	for(index = 0; index < udatalen; index++)
		//is the number there
		dataVal = _tcschr(digits, data[index] );
		if(dataVal != 0 )
			//if it is subtract where to start point
			digitValue = long(dataVal - digits);
			//increment Retval with digitvalue
			RetVal = RetVal * base + digitValue;
	//return the result
	return RetVal;

TCHAR* __fastcall NumToStr(TCHAR *RetData, long number, int base)
	long index = 0;
	const TCHAR numdigits[] = TEXT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	long digitValue = 0;
	TCHAR digits[sizeof(numdigits) + 1];
	TCHAR RetVal[512];
	TCHAR CurVal = 0;	
	ZeroMemory(RetVal, sizeof(RetVal));
	// only base supported are from 2 to 36
	if(base < 2 || base > 36 ) return NULL;
	ZeroMemory(digits, sizeof(digits));
	_tcsncpy(digits, numdigits, base);
		digitValue = number % base;
		number = number base;
		RetVal[index++] = digits[digitValue];
	//since string we have got is in reversed format
	//eg 100 will be 001 so we have to reverse it
	//and put the value in our variable
	ZeroMemory(RetData, _tcslen(RetVal)+1);
	int i = 0;
	for(index = _tcslen(RetVal) - 1; index > -1; index--)
		//start reversing
		RetData[i++] = RetVal[index];
	//return the result
	return RetData;

//our main function
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
	TCHAR Data[128];
	ZeroMemory(Data, sizeof(Data));
	//convert a number to string
	NumToStr(Data, 1123, 10);
	//now again convert string to number and see the result
	cout << StrToNum(Data, _tcslen(Data), 10) << endl;
	return 0;

Note: This code can also be used as a simple form of encryption. Here is a simple eg.:-

//convert string "1024" to a number of base 10;
_tcscpy(Data, TEXT("1024") );
int Number = StrToNum(Data, _tcslen(Data), 10);
//convert the number to a different base 27;
NumToStr(Data, Number, 27);
//now check the number
cout << TEXT("Modified String is ") << Data << endl;
//now again get back our original number
Number = StrToNum(Data, _tcslen(Data), 27);
NumToStr(Data, Number, 10);
cout << TEXT("Original Number is ") << Data << endl;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Shrishail Rana
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThanks Pin
Anil K P12-Sep-07 3:30
memberAnil K P12-Sep-07 3:30 
GeneralFrom&amp;To Pin
AMelnyk30-Dec-03 4:25
memberAMelnyk30-Dec-03 4:25 
QuestionWhat if it is not an integer? Pin
huanghui3-Jul-03 17:13
memberhuanghui3-Jul-03 17:13 
Generallltoa() unsigned __int64 Pin
Bamaco23-May-03 4:34
memberBamaco23-May-03 4:34 
Generaleasy way Pin
Anonymous27-Feb-02 8:37
memberAnonymous27-Feb-02 8:37 
GeneralExisting functions in the C library Pin
Tim Ranker7-Feb-02 10:46
memberTim Ranker7-Feb-02 10:46 
GeneralRe: Existing functions in the C library Pin
srana7-Feb-02 14:42
membersrana7-Feb-02 14:42 
GeneralRe: Existing functions in the C library Pin
Sven Axelsson7-Feb-02 23:18
memberSven Axelsson7-Feb-02 23:18 
GeneralRe: Existing functions in the C library Pin
Anonymous6-Nov-02 9:08
sussAnonymous6-Nov-02 9:08 
GeneralRe: Existing functions in the C library Pin
Sven Axelsson6-Nov-02 11:36
memberSven Axelsson6-Nov-02 11:36 
GeneralRe: Existing functions in the C library Pin
Artexerxes9910-Jul-02 9:20
memberArtexerxes9910-Jul-02 9:20 
Generalsprintf (szHex, "%X", number) Pin
Ravi Bhavnani6-Feb-02 19:42
memberRavi Bhavnani6-Feb-02 19:42 
GeneralRe: sprintf (szHex, "%X", number) Pin
solex7-Feb-02 4:08
membersolex7-Feb-02 4:08 
GeneralRe: sprintf (szHex, "%X", number) Pin
Anonymous9-Feb-02 20:09
memberAnonymous9-Feb-02 20:09 
GeneralRe: sprintf (szHex, "%X", number) Pin
Jörgen Sigvardsson10-Feb-02 3:50
memberJörgen Sigvardsson10-Feb-02 3:50 
GeneralRe: sprintf (szHex, "%X", number) Pin
Anonymous10-Feb-02 6:15
memberAnonymous10-Feb-02 6:15 
GeneralRe: sprintf (szHex, "%X", number) Pin
Rick York10-Feb-02 8:06
memberRick York10-Feb-02 8:06 
GeneralRe: sprintf (szHex, "%X", number) Pin
Sasha Djurovic10-Feb-02 10:04
memberSasha Djurovic10-Feb-02 10:04 
General(Isn't, like the previous) Re: sprintf (szHex, "%X", number) Pin
João Ferreira14-Feb-02 5:31
memberJoão Ferreira14-Feb-02 5:31 
GeneralRe: sprintf (szHex, "%X", number) Pin
Tim Ranker7-Feb-02 10:51
memberTim Ranker7-Feb-02 10:51 
GeneralRe: sprintf (szHex, "%X", number) Pin
Xelous11-Feb-02 0:55
memberXelous11-Feb-02 0:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 6 Feb 2002
Article Copyright 2002 by Shrishail Rana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid