0

I'm trying to create a code that converts a decimal into any base between 2 and 16. But I don't know how to write a new value in my string.

void base_conversion(char s[], int x, int b) {
int j, y;
j = 0;
// your code here
while(b > 1 && b < 17) {

    if (x < 0) {
        x = (-x);
    }else if(x == 0) {
        s[j] = '\0';
    }
        x = (x/b);

    while(x > 0) {

        y = (x%b);


        if (y == 10) {
            s[j] = 'A';
        }else if(y == 11) {
            s[j] = 'B';
        }else if(y == 12) {
            s[j] = 'C';
        }else if(y == 13) {
            s[j] = 'D';
        }else if(y == 14) {
            s[j] = 'E';
        }else if(y == 15) {
            s[j] = 'F';
        }else{
            s[j] = y;
        }
    }
}j = j + 1;
}
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
  • What research have you done on your question? – codingEnthusiast Feb 28 '15 at 17:27
  • First of all you increase `j` outside of any loop, so you will always overwrite `s[0]`. Secondly you're not modifying neither `x` nor `b` inside the inner loop, so if `x` is non-zero then you have an infinite loop. Thirdly, can you please elaborate on your problem (besides the ones I mention)? For some input, what is the expected and actual output? – Some programmer dude Feb 28 '15 at 17:27
  • You don't need a `while()` loop to test the base `b`: either the base is valid or it isn't - at the function entry. – Weather Vane Feb 28 '15 at 17:28
  • 1
    `while (b > 1 && b < 17) { /* code that does not change b and does not have a break statement */ }` is an infinite loop. – pmg Feb 28 '15 at 17:29
  • Also, please learn how to use a debugger. Stepping though the code line by line in a debugger would make all the problems I mention very obvious. – Some programmer dude Feb 28 '15 at 17:30
  • @naltipar I have managed to succesfully write a separate c that converts the number and prints out each character into a new line. But in this I need to use strings and reverse the string (i've already written a reverse code that works), which I am unsure of how I can change the value of a string at s[j] = 'Something' – Santiago Guerra Feb 28 '15 at 17:32
  • @WeatherVane So should I just completely remove the while( b > 1 && b < 17) – Santiago Guerra Feb 28 '15 at 17:35
  • @JoachimPileborg I am taking this course through my university and my professor hasn't taught us anything about debuggers. But I will definitely try and learn how to use one. Any that you recommend for Windows? – Santiago Guerra Feb 28 '15 at 17:48
  • Why are there a lot of `if`s in your code? Just `if (y >= 10 && y <= 15) s[j] = 'A' + y - 10; else s[j] = y;` is enough. Or in the worst case, use a `switch case` will increase readability a lot – phuclv Feb 28 '15 at 17:53

1 Answers1

1

You were almost there, although several mistakes, so I have "improved" your code. The infinite loop testing the base which needed to be done once only. The while() loops weren't quite organised right - x/b being done outside the digit extraction loop. Another change I made was to use a lookup array to convert each digit to a character, which saves a lot of laborious testing. I also returned the string passed as the function value - might as well add a tad more functionality. In the case of passing a bad base value, I could have returned NULL instead of an empty string. Note also I update j in the same statements where I use it as an index, which makes the code a little more fluent.

#include <stdio.h>

char *base_conversion (char *s, int x, int b) {
    char charr[] = "0123456789ABCDEF";
    int i, j = 0, len, digit, neg = 0;
    *s = 0;                     // terminate the string passed
    if (b < 2 || b > 16)        // check the base
        return s;               // return an empty string
    if (x < 0) {
        x = -x;                 // adjust for negative input
        neg = 1;
    }
    do {
        digit = x % b;          // extract each l.s. digit
        s[j++] = charr [digit]; // convert to character
    } while (x /= b);           // implicitly test for 0

    if (neg)                    // negative input
        s[j++] = '-';           // append a minus sign
    s[j] = 0;                   // terminate the string

    // reverse the string
    len = j;
    for (i=0; i<len/2; i++) {
        digit = s[i];
        s[i] = s[--j];          // pre-decrement j to next char index
        s[j] = digit;
    }
    return s;
}    

int main () { 
    int n;
    char strig[65];
    for (n=1000; n>=-1000; n-=2000) {
        printf ("Binary      %d: %s\n", n, base_conversion (strig, n, 2)); 
        printf ("Ternary     %d: %s\n", n, base_conversion (strig, n, 3)); 
        printf ("Octal       %d: %s\n", n, base_conversion (strig, n, 8)); 
        printf ("Decimal     %d: %s\n", n, base_conversion (strig, n, 10)); 
        printf ("Hexadecimal %d: %s\n", n, base_conversion (strig, n, 16)); 
    }
    return 0;
}

Program output:

Binary      1000: 1111101000
Ternary     1000: 1101001
Octal       1000: 1750
Decimal     1000: 1000
Hexadecimal 1000: 3E8
Binary      -1000: -1111101000
Ternary     -1000: -1101001
Octal       -1000: -1750
Decimal     -1000: -1000
Hexadecimal -1000: -3E8
Weather Vane
  • 33,872
  • 7
  • 36
  • 56