Get a character from its Unicode index

I’ve been working my way through these Java exercises at, and at one point I wanted to be able to get a particular Unicode character from its index. For example, I wanted to turn the integer 65 (or 41 in hexadecimal) into the unicode character U+0041, or a capital letter “A”. This was for a program which would convert a given decimal number to its hexadecimal equivalent:

import java.util.Scanner;

public class decimalToHex {
  public static void main(String[] args){
    Scanner in = new Scanner(;

    System.out.print("Insert a positive integer: ");
    int num = in.nextInt();
    String hex = "";
    int uni = 0;

    while(num != 0){
      uni = num % 16;
      if(uni > 9){ hex = Character.toString((char)(uni+55)) + hex; }
      else       { hex = Character.toString((char)(uni+48)) + hex; }
      num /= 16;



As you can see in the code above, the way I accomplished this was to typecast the integer values (uni+55 or uni+48) to a char, then convert that char to a String. Another way to do this (as was done in the example solution) is to define a 16-character or a 16-String array of 0-9 and A-F, and just add the appropriate character to “hex” with:

String[] myarray = { "0", "1", "2" ... "D", "E", "F" }
hex = myarray[uni] + hex;