How to convert negative numbers from decimal to hexadecimal

5

I was able to pass an integer number to hexadecimal. The problem now being tested was that the negative numbers do not work with this formula.

Here is the code I have:

public class PPROG_pl6_ex8 {

    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        int num;
        do {

            num = askNum();
            if (num == 0) {
                System.exit(0);
            } else {
                System.out.println(convertToHex(num));

            }
        } while (num != 0);
    }

    private static int askNum() {
        System.out.println("Introduza o número");
        int num = input.nextInt();
        return num;
    }

    public static String convertToHex(int num) {
        int base = 16;
        int alg;
        String valueOfAlg;
        String numero = "";

        do {
            alg = num % base;
            num = num / base;
            if (alg == 10) {
                valueOfAlg = "A";
            } else if (alg == 11) {
                valueOfAlg = "B";
            } else if (alg == 12) {
                valueOfAlg = "C";
            } else if (alg == 13) {
                valueOfAlg = "D";
            } else if (alg == 14) {
                valueOfAlg = "E";
            } else if (alg == 15) {
                valueOfAlg = "F";
            } else {
                valueOfAlg = String.valueOf(alg);
            }

            /*
            PARA TESTAR!
            System.out.println("alg: " + alg);
            System.out.println("num: " + num);
            System.out.println("valueOfAlg: " + valueOfAlg);
            System.out.println("numero: " + numero);
             */
            numero = valueOfAlg + numero;
        } while (num != 0);
        return numero;
    }
}

I also tried to put a condition for the negative numbers:

if(alg == -10)

 valueOfAlg = "-A"

... and up to 15 below

But it also did not work because it had numbers like -f-f .

I need help translating numbers into negatives.

    
asked by anonymous 27.10.2017 / 14:08

2 answers

4

You can use a Boolean flag negativo to crawl this, remove the sign from the number and at the end put "-" in front if the flag is true.

In addition, in the cases of digits 10 to 15 (A to F), you can eliminate the need for this if s by using an array of strings containing the digits. This array is built like this:

    private static final String[] HEX_DIGITS = "0123456789ABCDEF".split("");

Here's the full code:

import java.util.Scanner;

class PPROG_pl6_ex8 {

    public static final Scanner input = new Scanner(System.in);

    private static final int BASE = 16;
    private static final String[] HEX_DIGITS = "0123456789ABCDEF".split("");

    public static void main(String[] args) {
        int num;
        do {
            num = askNum();
            System.out.println(num + " -> " + convertToHex(num));
        } while (num != 0);
    }

    private static int askNum() {
        //System.out.println("Introduza o número");
        return Integer.parseInt(input.nextLine());
    }

    public static String convertToHex(int num) {
        if (num == 0) return "0";
        boolean negativo = num < 0;
        if (negativo) num = -num;
        String resposta = "";

        do {
            int alg = num % BASE;
            num /= BASE;
            String valueOfAlg = HEX_DIGITS[alg];
            resposta = valueOfAlg + resposta;
        } while (num != 0);

        if (negativo) resposta = "-" + resposta;
        return resposta;
    }
}

Here is the output produced:

1 -> 1
2 -> 2
3 -> 3
4 -> 4
9 -> 9
10 -> A
11 -> B
15 -> F
16 -> 10
31 -> 1F
50 -> 32
256 -> 100
513 -> 201
64876487 -> 3DDEFC7
-1 -> -1
-2 -> -2
-15 -> -F
-16 -> -10
-255 -> -FF
-256 -> -100
-47839 -> -BADF
-3473732 -> -350144
0 -> 0

See here working on ideone.

    
27.10.2017 / 15:16
2

Just leaving an alternative answer for anyone who can use the Java APIs. One of the variations of Integer.toString accepts a radix parameter.

System.out.println(Integer.toString(15, 16).toUpperCase()); // F
System.out.println(Integer.toString(-15, 16).toUpperCase()); // -F
    
27.10.2017 / 15:33