问题
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
回答1:
You just can't store them with leading zero. Leading zero to an integer indicates that it's an octal number.
Since your next step is XOR, I recommend this approach.
- You can store these integers using simple base 10 numbers. We will convert them when needed as binary. (you can also store them simply as binary with leading
0b. See this answer for more details. - Use
Integer.toString(hm.get(s), 2);to display binary number. The original number is still an Integer so you can use it for XOR operation. For displaying binary with leading zero, I've played with some string methods like this:
String temp = "000", binary; for( String s : input ) { binary = Integer.toString(hm.get(s), 2); System.out.print(temp.substring(0, 3-binary.length()) + binary +" "); }
Here's what the final code looks like:
Output
Binary form of text is ....
001 000 010 100 001 010 111 100 000 101
Code
import java.util.*;
public class onetimepad {
public static void main(String args[]) {
HashMap <String , Integer>hm = new HashMap <String , Integer> ();
hm.put("e", 0); //or use hm.put("e", 0b000);
hm.put("h", 1); //or use hm.put("e", 0b001);
hm.put("i", 2);
hm.put("k", 3);
hm.put("l", 4);
hm.put("r", 5);
hm.put("s", 6);
hm.put("t", 7);
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 ....");
String temp = "000", binary;
for( String s : input ) {
binary = Integer.toString(hm.get(s), 2);
System.out.print(temp.substring(0, 3-binary.length()) + binary +" ");
}
}
}
回答2:
First, your Map declaration and initialization is a bit off. To use binary constants you prefix it with 0b - and please program to the Map interface (not the HashMap implementation). And, since Java 7, you can use the diamond operator <> to shorten things up.
Map<String, Integer> hm = new HashMap<>();
hm.put("e", 0b000);
hm.put("h", 0b001);
hm.put("i", 0b010);
hm.put("k", 0b011);
hm.put("l", 0b100);
hm.put("r", 0b101);
hm.put("s", 0b110);
hm.put("t", 0b111);
Then, for printing, you have Integer(s) but you want their binary representation. So you can do something like,
for (String s : input) {
System.out.print(Integer.toBinaryString(hm.get(s)) + " ");
}
Which I ran to get (as I believe you expected)
Binary form of text is ....
1 0 10 100 1 10 111 100 0 101
If you really want the leading zeros (a three bit binary format) you could do,
for (String s : input) {
StringBuilder sb = new StringBuilder(Integer.toBinaryString(hm.get(s)));
while (sb.length() < 3) {
sb.insert(0, '0');
}
System.out.print(sb.append(" "));
}
Which outputs
Binary form of text is ....
001 000 010 100 001 010 111 100 000 101
回答3:
Thanks for your inputs guys . I have finished one part of my problem , that required me to encrypt my input text : "Heilhitler " and present it in the binary format . This was the code i was able to build with your suggestions .
public static void main(String args[])
{
HashMap <String , Integer>hm = new HashMap <String , Integer> ();
hm.put("e", 0);
hm.put("h",1);
hm.put("i", 2);
hm.put("k",3);
hm.put("l",4);
hm.put("r",5);
hm.put("s",6);
hm.put("t",7);
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[] inter1 = new int[10];
System.out.println("Binary form of text is ....");
int i = 0 ;
for( String s : input )
{
String binarystr = Integer.toBinaryString(hm.get(s)) ;
System.out.print( binarystr+" ");
inter1[i]=Integer.parseInt(binarystr) ;
i++ ;
}
int[] inter2 = new int[10];
int m= 0 ;
for( String s : key )
{
String binarystr = Integer.toBinaryString(hm.get(s)) ;
System.out.print( binarystr+" ");
inter2[m]=Integer.parseInt(binarystr) ;
m++ ;
}
int[] cipher = new int[10];
for(int j = 0 ; j < 10 ; j++)
{
cipher[j] = inter1[j] ^ inter2 [j]; //performing xor between input and key
}
System.out.println("Cipher is .....");
for( int j= 0 ; j < 10; j++ )
{
System.out.print(" " + cipher[j]);
}
//-------------------Decryption //----------------------------
//Performing XOR between the cipher and key again
int[] decry = new int[10] ;
for(int j = 0 ; j < 10 ; j ++ )
{
decry[j] = cipher[j] ^ inter2[j];
}
System.out.println(" ");
System.out.println("Decrypted result in Binary format");
for( int j= 0 ; j < 10; j++ )
{
System.out.print(" " + decry[j]);
}
}
}
Output:
Binary form of text is ....
1 0 10 100 1 10 111 100 0 101
Cipher is .....
110 101 100 1 110 110 111 1 110 101
Decrypted result in Binary format
1 0 10 100 1 10 111 100 0 101
来源:https://stackoverflow.com/questions/41949664/storing-charcter-and-binary-number-in-a-hash-map