Search code examples
javahashmapbinarynumbersoctal

Java HashMap keys are not outputting their correct values


So I'm trying to a build a basic program for hexadecimal to binary conversion just using a hashmap. For whatever reason, every key that is below '8' is outputting a value that is not the binary. Ex: 5 should output 0101, but instead it outputs 65. Below is basically the hash.

    map.put('1', 0001);
    map.put('2', 0010);
    map.put('3', 0011);
    map.put('4', 0100);
    map.put('5', 0101);
    map.put('6', 0110);
    map.put('7', 0111);
    map.put('8', 1000);
    map.put('9', 1001);
    map.put('A', 1010);
    map.put('B', 1011);
    map.put('C', 1100);
    map.put('D', 1101);
    map.put('E', 1110);
    map.put('F', 1111);

    System.out.println(map.get('5'));

I'm crafting a work around, but I'm really curious as to why this is happening.


Solution

  • A - Reason

    Because on the following line, you are using the Octal representation because you are starting with 0. In the base-8 Octal represantation, 0101 stands for 65, and because that you map Character 5 to that value, the integer value of the octal 0101 computed as below;

    1*8^2 + 0*8^1* + 1*8^0 = 64 + 0 = 1 = 65

    You need to map the values in integer format, no need to use octal or binary. The values does not change but the representation is changed.

    B - Demo Code For Fail

    package com.levo.so.maprelated;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class HexMapFailDemo {
    
        public static void main(String[] args) {
            Map<Character, Integer> map = new HashMap<>();
    
            map.put('1', 0001);
            map.put('2', 0010);
            map.put('3', 0011);
            map.put('4', 0100);
            map.put('5', 0101);
            map.put('6', 0110);
            map.put('7', 0111);
            map.put('8', 1000);
            map.put('9', 1001);
            map.put('A', 1010);
            map.put('B', 1011);
            map.put('C', 1100);
            map.put('D', 1101);
            map.put('E', 1110);
            map.put('F', 1111);
    
            System.out.println(map.get('5'));
            System.out.println("Octal 101 = 8^2 + 1 = " + (8*8 + 1));
        }
    
    }
    

    C - Output

    65
    Octal 101 = 8^2 + 1 = 65
    

    D - Successful Demo Code

    package com.levo.so.maprelated;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class HexMapSuccessDemo {
    
        public static void main(String[] args) {
            Map<Character, Integer> map = new HashMap<>();
    
            map.put('1', 1);
            map.put('2', 2);
            map.put('3', 3);
            map.put('4', 4);
            map.put('5', 5);
            map.put('6', 6);
            map.put('7', 7);
            map.put('8', 8);
            map.put('9', 9);
            map.put('A', 10);
            map.put('B', 11);
            map.put('C', 12);
            map.put('D', 13);
            map.put('E', 14);
            map.put('F', 15);
    
            System.out.println(map.get('5'));
        }
    
    }
    

    E - Output

    5