I am trying to preserve a mapping of letters to a binary number. Here is my mapping
("h",001)
("i", 010)
("k",011)
("l",100)
("r", 101)
("s",110)
("t",111)
To this end, I created a hash map and saved key value pairs. Now I want to display the corresponding binary value for this sentence. Here is my code for the same.
package crups;
import java.util.*;
public class onetimepad {
public static void main(String args[])
{
HashMap <String , Integer>hm = new HashMap <String , Integer> ();
hm.put("e", 000);
hm.put("h",001);
hm.put("i", 010);
hm.put("k",011);
hm.put("l",100);
hm.put("r", 101);
hm.put("s",110);
hm.put("t",111);
String[] key = { "t" ,"r" , "s" , "r","t","l","e", "r","s","e"};
String[] input = {"h","e","i" ,"l","h","i","t","l","e","r"};
int[] cipher = new int[10];
System.out.println("Binary form of text is ....");
for( String s : input )
{
System.out.print(hm.get(s)+" ");
}
}
}
However, when I run the code, the display for the letter "i" does not display correctly 8:: instead 010. Can someone tell me why this is happening? Also how can I display a zero infront of my numbers since they are binary numbers. Thank.
Output:
Binary form of text is ....
1 0 8 100 1 8 111 100 0 101