Cassie
Cassie

Reputation: 3099

Convert from decimal to binary, octal, hexadecimal with char array

I have converted some decimal numbers into the binary, octal and hexadecimal system. For that, I didn't have to use any collections and libraries. So now I need to change my implementation because I used String for storing the result, while I supposed to use a char array.

My current implementation:

public static String convertDecimal(int number, int base){
    String result = "";
    while (number > 0) {
        if (base == 16) {
            int hexalNumber = number % base;

            char hexDigit = (hexalNumber <= 9 && hexalNumber > 0) ? 
                    (char) (hexalNumber + '0') : 
                    (char) (hexalNumber - 10 + 'A');
            result = hexDigit + result;
            number = number / base;
        }
        if (base == 8 || base == 2) {
            int remainder = number % base;
            result = remainder + result;
            number = number / base;
        }
    }
    return result;
}

How can I change my implementation in order to return char[] from the method? Should I completely change the logic of my conversion algorithm?

I would be grateful for any help.

Upvotes: 2

Views: 1494

Answers (3)

canillas
canillas

Reputation: 424

I have tested this in your code:

convertDecimal(7856421, 16);

For this call and editing your code as following (I have not deleted any line, only added I have added the car[]):

public static String convertDecimal(int number, int base) {
char[] resultChar = new char[0];
String result = "";

while (number > 0) {
    if (base == 16) {
    int hexalNumber = number % base;

    char hexDigit = (hexalNumber <= 9 && hexalNumber > 0) ? (char) (hexalNumber + '0')
        : (char) (hexalNumber - 10 + 'A');
    result = hexDigit + result;
    int totalLength = resultChar.length + 1;
    char[] aux = new char[totalLength];
    int i = 0;
    for (i = 0; i < resultChar.length; i++) {
        aux[i] = resultChar[i];
    }
    aux[totalLength - 1] = hexDigit;
    resultChar = aux;

    number = number / base;
    }
    if (base == 8 || base == 2) {
    int remainder = number % base;
    result = remainder + result;

    number = number / base;
    }

}
return result;
}

Yo have in resultChar[] the result inverted (in this case you get 77E125 and the char[] has [5, 2, 1, E, 7, 7]). now you only need to return it inverted.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

If they want an array of char, they probably won't like just a

return result.toCharArray();

As arrays are of fixed size, one could first count the digits:

public static char[] convertDecimal(int number, int base) {
    if (number < 0) {
        char[] positiveResult = convertDecimal(-number, base);
        char[] negativeResult = ...
        return negativeResult;
    } else if (number == 0) {
        return new char[] { '0' };
    }
    int digits = 0;
    int n = number;
    while (n != 0) {
        ++digits;
        n /= base;
    }
    char[] result = new char[digits];
    for (int i = 0; i < digits; ++i) {
        ... result[... i ...] = ...; ++i
    }
    return result;
}

Upvotes: 1

Schidu Luca
Schidu Luca

Reputation: 3947

String has a method for converting itself to a char[], just use it:

return result.toCharArray();

Upvotes: 1

Related Questions