Question

This program is to convert a base 4 number to a base 2 number and it should be done in place

#include<stdio.h>
#include<string.h>

void shiftr(char num[],int i)
{
     memmove(num+i,num+i+1,strlen(num)-i);
}

char* convert4to2(char num[])
{
int i=0,len;
char ch;

    while(num[i]!='\0')
    {
        ch=num[i];
        shiftr(num,i);
        switch(ch)
        {
            case '0':num[i++]='0';
                 num[i++]='0';
                 break;

            case '1':num[i++]='0';
                 num[i++]='1';
                 break;

            case '2':num[i++]='1';
                 num[i++]='0';
                 break;

            case '3':num[i++]='1';
                 num[i++]='1';
                 break;

            default:printf("Error");
        }
    }

    num[i]='\0';
    return(num);
}



void main()
{
    char num[20];
    printf("Enter the Base 4 Number:");
    scanf("%s",&num);
    printf("The Binary Equivalent is:%s\n",convert4to2(num));   
}

The output for an input of 121(base 4 number) should be 011001 but its displaying only 01. And for larger numbers like 12101 it displays 0100 taking the first and the last but one numeral. What could be the problem?

Was it helpful?

Solution

You're actively destroying your input. E.g., for the first iteration, you shift your number by one place and after that you overwrite the data at place 0 and place 1 (which contains the next 2 digits in 4-base) with your binary output for the 1st digit.

OTHER TIPS

Instead of converting directly from base 4 to base 2 using characters, you could put the string through the strtol function, which converts integer string of arbitrary base to a long. From there it's pretty easy to print out the binary representation.

Edit: Example:

char* convert4to2(const char input[], char *output, const size_t output_length)
{
    /* First make sure the output string is long enough */
    if (output_length < (sizeof(long) * 8 + 1))  /* +1 for '\0' */
        return NULL;  /* Not enouth space */

    /* Convert from the input string to a `long` */
    long value = strtol(input, NULL, 4);  /* The last `4` is the base of the input string */

    /* Convert the number to binary */
    char *output_ptr = output;

    /* Multiply with 8 to get the number of bits */
    /* Subtract 1 because bits a numbered from zero */
    for (int bit = sizeof(long) * 8 - 1; bit >= 0; bit--)
    {
        /* `value >> bit` make the current bit the lowest bit */
        /* `& 1` to mask out all but the lowest bit */
        /* `+ '0'` to make it a proper character digit */
        *output_ptr++ = ((value >> bit) & 1) + '0';
    }

    /* Terminate the string */
    *output_ptr = '\0';

    /* Return the converted string */
    return output;
}

Call it like this:

const char num[] = "121";
char output[65];  /* `long` can be 64 bits, plus one for the string terminator */

printf("%s in base 4 is %s in base 2\n",
    num, convert4to2(num, output, sizeof(output)));

No need to shift upward, just work backward:

#include <stdio.h>
#include <string.h>

void convert4to2(char *str)
{
size_t idx;
char ch;
static char *conv[4] = {"00", "01","10", "11" };

idx = strlen(str);
str[idx*2] = 0;

while(idx--) {
    ch=str[idx];
    if (ch < '0' || ch > '3') return; /* replace by relevant error handler */
    memcpy(str+2*idx, conv[ ch - '0' ], 2 );
    }

return;
}

int main(void)
{
char quad[21] = "0123321023" ;

printf("Old:%s\n", quad);
convert4to2(quad);
printf("New:%s\n", quad);

return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top