Converting Between Different Number Bases

Environment: Win32, C++

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
  _tcsupr(data);
  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);
  while(number)
  {
    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;

That's All.

Please visit my site for more Tips and tweaks.



Comments

  • Number System

    Posted by Legacy on 12/02/2003 12:00am

    Originally posted by: Aliya Wajahat

    I found it good enough.

    Reply
  • What about STLs

    Posted by Legacy on 02/19/2002 12:00am

    Originally posted by: Ferar Achkar

    U should check the latest support for converting and dealing with other Base numbers in STLs, Borland for instance has a very good library for these operations which are much better and much efficient.

    Reply
  • What is wrong with strtol()?

    Posted by Legacy on 02/17/2002 12:00am

    Originally posted by: Bruce

    The standard C function strtol() will do this and if you want unicode, wcstol() will do it. Am I missing something?

    Reply
  • Type O ?

    Posted by Legacy on 02/16/2002 12:00am

    Originally posted by: Roger McElfresh

    line: 59 number = number base;
    
    S/B: number = number / base;

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds