在散列映射中存储charcter和二进制数

时间:2017-01-31 04:30:32

标签: java binary hashmap

我正在尝试将字母映射存储到二进制数字。这是我的映射

("h",001)
("i", 010)
("k",011)
("l",100)
("r", 101)
("s",110)
("t",111)

为此,我创建了一个哈希映射并存储了键值对。我现在想要为给定的句子显示相应的二进制值。这是我的代码。

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)+" ");
    }

}   

}

然而,当我运行代码时,字母的映射&#34; i&#34;显示错误:8:而不是010。 有人可以告诉我为什么会这样吗?另外,我如何显示我的数字前面的零,因为这些是二进制数字。 感谢。

输出:

Binary form of text is ....
1 0 8 100 1 8 111 100 0 101 

3 个答案:

答案 0 :(得分:2)

你无法用前导零存储它们。从零到整数表示它是一个八进制数。

由于您的下一步是异或,我推荐这种方法。

  1. 您可以使用简单的基数10来存储这些整数。我们将在需要时将它们转换为二进制。 (您也可以将它们简单地存储为带有0b的二进制文件。See this answer for more details.
  2. 使用Integer.toString(hm.get(s), 2);显示二进制数。原始数字仍然是整数,因此您可以将其用于XOR操作。
  3. 为了显示前导零的二进制文件,我使用了一些字符串方法:

    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 +" ");
    }
    
  4. 这里是最终代码的样子:

    <强>输出

    Binary form of text is ....
    001 000 010 100 001 010 111 100 000 101 
    

    <强>代码

    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 +" ");
            }
        }   
    }
    

答案 1 :(得分:2)

首先,您的Map声明和初始化有点过时了。要使用二进制常量,请在其前面添加0b - 并请编程到Map接口(而不是HashMap实现)。而且,从Java 7开始,您可以使用菱形运算符<>缩短内容。

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);

然后,对于打印,您有Integer(s)但是您想要它们的二进制表示。所以你可以做点什么,

for (String s : input) {
    System.out.print(Integer.toBinaryString(hm.get(s)) + " ");
}

我跑去了(我相信你的预期)

Binary form of text is ....
1 0 10 100 1 10 111 100 0 101 

如果您真的想要前导零(三位二进制格式),那么

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(" "));
}

哪个输出

Binary form of text is ....
001 000 010 100 001 010 111 100 000 101 

答案 2 :(得分:0)

感谢您的投入。 我已经完成了我的问题的一部分,这要求我加密我的输入文本:“Heilhitler”并以二进制格式呈现它。这是我能够根据您的建议构建的代码。

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]);
    }

    }
  }

输出:

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