javaunicodehexunicode-escapesunicode-literals

Get the Unicode from a Hexadecimal


I've been searching for a solution to my problem for days but can't get a spot-on answer when looking at previously answered questions/ blogs / tutorials etc. all over the internet.

My aim is to write a program which takes a decimal number as an input and then calculates the hexadecimal number and also prints the unicode-symbol of said hexadecimal number (\uXXXX). My problem is I can't "convert" the hexadecimal number to unicode. (It has to be written in this format: \uXXXX)

Example:

Input:

122 (= Decimal)

Output:

Hexadecimal: 7A

Unicode: \u007A | Unicode Symbol: Latin small letter "z"

The only thing I've managed to do is print the unicode (\u007A), but I want the symbol ("z"). I thought if the unicode only has 4 numbers/letters, I would just need to "copy" the hexadecimal into the code and fill up the remaining places with 0's and it kinda worked, but as I said I need the symbol not the code. So I tried and tried, but I just couldn't get the symbol. By my understanding, if you want the symbol you need to print it as a string. But when trying it with a string I get the error "illegal unicode escape".

It's like you only can print pre-determined unicodes and not "random" ones generated on the spot in relation of your input.

I'm only a couple days into Java, so apologies if I have missed anything.

Thank you for reading.

My code:

        int dec;
        int quotient;
        int rest;
        int[]hex = new int[10];
        char[]chars = new char[]{
            'F',
            'E',
            'D',
            'C',
            'B',
            'A'
        };
        String unicode;
// Input Number
        System.out.println("Input decimal number:");
        Scanner input = new Scanner(System.in);
        dec = input.nextInt();
//
// "Converting to hexadecimal
        quotient = dec / 16;           
        rest = dec % 16;
        hex[0] = rest;

        int j = 1;
        while (quotient != 0) {
            rest = quotient % 16;
            quotient = quotient / 16;
            hex[j] = rest;
            j++;
        }
//

        /*if (j == 1) {
            unicode = '\u000';
        }
        if (j == 2) {
            unicode = '\u00';
        }
        if (j == 3) {
            unicode = '\u0';
        }*/

        System.out.println("Your number: " + dec);
        System.out.print("The corresponding Hexadecimal number: ");

        for (int i = j - 1; i >= 0; i--) {
            if (hex[i] > 9) {
                if (j == 1) {
                    unicode = "\u000" + String.valueOf(chars[16 - hex[i] - 1]);
                }
                if (j == 2) {
                    unicode = "\u00" + String.valueOf(chars[16 - hex[i] - 1]);
                }
                if (j == 3) {
                    unicode = "\u0" + String.valueOf(chars[16 - hex[i] - 1]);
                }
                System.out.print(chars[16 - hex[i] - 1]);
            } else {
                if (j == 1) {
                    unicode = "\u000" + Character.valueOf[hex[i]);
                }
                if (j == 2) {
                    unicode = "\u00" + Character.valueOf(hex[i]);
                }
                if (j == 3) {
                    unicode = "\u0" + Character.valueOf(hex[i]);
                }
                System.out.print(hex[i]);
            }
        }

        System.out.println();
        System.out.print("Unicode: " + (unicode));
    }

It's not an advanced code whatsoever, I wrote it exactly how I would calculate it on paper. Dividing the number through 16 until I get a 0 and what remains while doing so is the hexadecimal equivalent. So I put it in a while loop, since I would divide the number n-times until I got 0, the condition would be to repeat the division until the quotient equals zero. While doing so the remains of each division would be the numbers/letters of my hexadecimal number, so I need them to be saved. I choose an integer array to do so. Rest (remains) = hex[j]. I also threw a variable in the called "j", so I would now how many times the division was repeated. So I could determine how long the hexadecimal is. In the example it would 2 letters/numbers long (7A), so j = 2. The variable would then be used to determine how many 0's I would need to fill up the unicode with. If I have only 2 letters/numbers, it means there are 2 empty spots after \u, so we add two zeros, to get \u007A instead of \u7A.

Also the next if-command replaces any numbers higher than 9 with a character from the char array above. Basically just like you would do on paper.

I'm very sorry for this insanely long question.


Solution

  • You can simply use System.out.printf or String.format to do what you want.

    Example:

    int decimal = 122;
    
    System.out.printf("Hexadecimal: %X\n", decimal);
    System.out.printf("Unicode: u%04X\n", decimal);
    System.out.printf("Latin small letter: %c\n", (char)decimal);
    

    Output:

    Hexadecimal: 7A
    Unicode: u007A
    Latin small letter: z