## Thursday, September 29, 2011

In the article Formatted Strings in C I've given a few examples on how to convert between hexadecimal, octal and decimal.

Still, by using the methods described in that article you cannot convert directly to binary or a more "exotic" radix.

Description:
A solution lays in the function char* itoa(int inputValue, char *result, int radix). This function allows you to take a number in decimal, octal or hexadecimal and convert it to a string containing the number's representation in a radix of your choosing.

Example:
```#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int number = 16;
printf("\nBinary  : %s"
return 0;
}
/*Output:
Binary  : 10000
*/
```
Pitfall:
The function is not ANSI, so it will not available on all compilers. Still, you can implement it by yourself. Check the useful links at the end of the article.

Observation:
This function was present in most of the old compilers (ex. Borland C) and some regard it as deprecated.

Description:
Another option for radix conversion is the ANSI function long int strtol ( const char * str, char * endptr, int radix). The function takes as arguments:
endptr - a pointer who shall memorize the position of last character after the conversion

Simple example:
```#include<stdio.h>
#include<stdlib.h>

int main(void)
{
long x = strtol("zzzz",NULL,36);
printf("%ld\n",x);
return 0;
}
/*Output:
1679615
*/```
In the example above, we convert the number zzzz (represented in radix 36) to a number in radix 10.

```#include<stdio.h>
#include<stdlib.h>

int main(void)
{
char buffer[] = "2050 40a0b0 -110111 zzzz";
char * ptr_end = NULL;
long int d1, d2, d3, d4;
d1 = strtol (buffer,&ptr_end,10);
d2 = strtol (ptr_end,&ptr_end,16);
d3 = strtol (ptr_end,&ptr_end,2);
d4 = strtol (ptr_end,NULL,36);
printf ("%ld, %ld, %ld, %ld\n", d1, d2, d3, d4);
return 0;
}
/*Output:
2050, 4235440, -55, 1679615
*/```
In this example we take full advantage of the function's capabilities. The function will use ptr_end to return a pointer after a conversion to the end of the substring containing the number who was converted.

```/*
* Description:
* Parameters:
*  oldNumber - a string containing the number who will be converted
*  oldradix - the base of number
*  newNumber - a string who shall contain the value of number in
* Preconditions:
*  The parameters base and newBase must be between 1 and 36
*  The number arguments can only contain '0' - '9' and 'A'-'Z' chars
*  The number string must represent correctly the number in base "base"
* Postcondtions
*  The function returns:
*      1 - Success
*      0 - Failure because bases were not correct
*      -1 - Failure because newNumber incorrect format
*
*/
{
long data = 0L;
char digit = 0;
int i = 0;
int size=strlen(oldNumber);
char* reverseNew = NULL;
/*Checks if base if within bounds*/
{
return 0;
}
/*Convert to base 10 from old base*/
for(i=0;i<size;i++)
{
if(oldNumber[i]>='0' && oldNumber[i]<='9')
{
}
else if(oldNumber[i]>='A' && oldNumber[i]<='Z')
{
}
else
{
return -1;
}
}
i=0;
/*Convert from base 10 to new base*/
while(data>0)
{
(digit<10)?(newNumber[i] = digit + '0')
:(newNumber[i] = digit + 'A' -10);
i++;
}
newNumber[i]='\0';
/*Reverses newNumber*/
reverseNew = (char*)(malloc(sizeof(char)*strlen(newNumber)));
size = strlen(newNumber);
for(i=0; i<size; i++)
{
reverseNew[i] = newNumber[size-1-i];
}
reverseNew[size]='\0';
newNumber = reverseNew;
return 1;
}```
The function above converts number to radix 10 and then converts the result the the specified newRadix radix.

Example:
```#include<stdio.h>
#include<stdlib.h>

int main(void)
{
char data="1233";
char data2;
/*Converts from base 5 to base 3*/
return 0;
}
/*Output: 