A program to reverse number of any base to other base

Page 1 of 1

0 Replies - 454 Views - Last Post: 03 April 2007 - 01:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=364221&amp;s=018f0511db3e50e8a1c4a0177cdc4ea2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

A program to reverse number of any base to other base

Posted 03 April 2007 - 01:57 AM

Description: Works till max 16 base.This Program will help in situations where you don't know the base equivalant of a number and you want to reverse base equivalant of the number.
```/* This program accepts number in any base and reverses it for other base.
* This mean if you enter binary 110 and reversing it for base 16 then
* answer will be 6 and not 4, because we reverse it for base 16 and not
* for base 2. for base 2 it will show 011. For values like 10010 reverse
* in hex would be 21 as number in hex for 10010 is 12.
*/

/* This Program will help in situations where you don't know the base
* equivalant of a number and you want to reverse base equivalant of the number.
* e.g. if you don't know binary of 18 and you want to reverse binary of 18.
* then this program directly gives reversed binary of 18 i.e. 01001.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>

int convertToDec(char number[], int base);
int main()
{
int no,base1,base2,i=0;
char rev[30],num[30];
char arr[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
//The number to be converted
printf("nEnter The Number : ");
scanf("%s",num);
//The base of entered number
printf("nEnter The Base Of Number : ");
scanf("%d",&base1);
//the base for which number is to be reversed.
printf("nEnter The Base In Which The Number Should Be Reversed : ");
scanf("%d",&base2);
//make it decimal first
no = convertToDec(num,base1);
//let's reverse it now
while(no>0)
{
rev[i] = arr[(no % base2)];
no /=base2;
i++;
}
rev[i]='�';
printf("nReversed Number is : %sn",rev);
return 0;
}

int convertToDec(char number[], int base)
{
int decValue =0,i=strlen(number)-1,val,j=0;
while(i>=0)
{
//making char to int [can have a better logic]
switch(number[i])
{
case '0' : val =0; break;
case '1' : val =1; break;
case '2' : val =2; break;
case '3' : val =3; break;
case '4' : val =4; break;
case '5' : val =5; break;
case '6' : val =6; break;
case '7' : val =7; break;
case '8' : val =8; break;
case '9' : val =9; break;
case 'A' :
case 'a' : val =10; break;
case 'B' :
case 'b' : val =11; break;
case 'C' :
case 'c' : val =12; break;
case 'D' :
case 'd' : val =13; break;
case 'E' :
case 'e' : val =14; break;
case 'F' :
case 'f' : val =15; break;
}
//make a decimal part from it.
decValue += ((int)pow(base,j)*val);
j++;
i--;
}
return decValue;
}

```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }