I am trying to store 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)
For this purpose, I have created a hash map and stored the key value pairs. I now want to display the corresponding binary value, for a given 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"};
//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)+" ");
}
}
}
When I run the code however, the mapping for the letter "i" is shown wrong : 8
: instead of 010
.
Can some one please tell me why this is happening? Also how can I display the zeroes infront of my numbers, as these are binary numbers.
Thanks.
Output :
Binary form of text is ....
1 0 8 100 1 8 111 100 0 101