All right. So I'm taking an assembly language class, but we apparently also need to use a smattering of Unix and C. Our assignment is to create a function that converts from the input base to a listed base.

Example of a call: ConvertToBase(057,10) should return 47 (the input was in octo, and decimal was requested).

The "easy" part is that this isn't an interactive program. I simply need to create the function and have the program use the function on a few numbers (using a UNIX typescript to show that it's a program that works and not just spitting out what i tell it to).

I understand that I need to create an array with all the characters I need, so:

char baseChar[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

I *THINK* this should work to convert the value:

while (inputInt != 0) 
	{ 
		convertedInt[index] = inputInt % baseInt; 
		inputInt = inputInt / baseInt; 		
		++index; 
	}

and then I *THINK* something like this would print the values in reverse order to the desired base

--index; 
	
	printf("\n\nConverted Number = "); 
	
	for( ; index>=0; index--) 
	{ 
		printf("%c", baseChar[convertedInt[index]]); 
	} 
	printf("\n");

Unfortunately, this doesn't really seem to lend itself to a function. It's frustrating because I think I can write a program that does what I need, but I can't really make a function to do it. Can anyone help me out?

we apparently also need to use a smattering of Unix and C

Sounds messy.

Unfortunately, this doesn't really seem to lend itself to a function.

Why not? You already have the call, ConvertToBase(057,10), which immediately gives you ideas for the function signature:

void ConvertToBase(unsigned inputInt, unsigned baseInt);

Your thoughts on the implementation are sound, so just put it all together with the necessary local variables:

void ConvertToBase(unsigned inputInt, unsigned baseInt)
{
    char baseChar[16] = {
        '0', '1', '2', '3', '4', '5', '6', '7', 
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
    };
    int convertedInt[50];
    int index = 0;

    while (inputInt != 0) {
        convertedInt[index] = inputInt % baseInt;
        inputInt = inputInt / baseInt;
        ++index;
    }

    --index;

    printf("\n\nConverted Number = ");

    for( ; index >= 0; index--) {
        printf("%c", baseChar[convertedInt[index]]);
    }
    
    printf("\n");
}

when You write zero before any decmial number, compiler will automatically convert that number into its octal form , check the code:-

#include <stdio.h>
int main()
{
   printf("%d",057);
        return 0;
}

however i tried a lot to understand your code but i cant get why you are using an array.
I tried a little bit , hope this may help you.

#include <stdio.h>
int pow(int , int);
int octconv(int , int);
int main()
{
    printf("%d",octconv(57,10));
        return 0;
}
int octconv(int oct , int base)
{
     int num=0,temp,exp=0;
     while(oct>0)
     {
         temp=oct%10;
         num=num+temp*pow(8,exp);
         oct/=10;
         exp++;
     }
     return(num);
}

int pow(int n, int exp)
{
    int i,power;
    if(exp==0)return 1;
    for(i=1;i<exp;i++)  n*=n;
    return n;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.