How to convert negative decimal numbers to hexadecimal

Asked

Viewed 1,339 times

5

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

This 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 to the negative numbers:

if(alg == -10)

 valueOfAlg = "-A"

... and forward up to 15

But I couldn’t because I had numbers like -f-f.

I need help converting the numbers to negatives.

  • Related: https://answall.com/q/248453/64969

2 answers

4


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

In addition, in the case of digits 10 to 15 (A to F), it is possible to eliminate the need for this ifs when using an array of strings containing 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.

2

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.