【问题标题】:storing charcter and binary number in a hash map在哈希图中存储字符和二进制数
【发布时间】:2017-06-16 10:08:14
【问题描述】:

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

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

}   

}

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

输出:

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

【问题讨论】:

  • 将它们存储为字符串 ("001") 而不是数字。
  • 这也是一些有趣的输入文本。
  • @LukeBriggs:哈哈,这显然是一个很容易形成不同句子的文本,通过排列。
  • 您在源代码中查看的数字是十进制的 - 例如,101 是“一百零一”。你会选择代表你想要的二进制的十进制数字。 (例如十进制的4 是二进制的100),然后使用converting an int to a binary string 之类的东西来方便地显示位:)
  • @shmosel 将它们存储为字符串 ("001") 而不是数字。 现在谁在作弊?

标签: java binary hashmap


【解决方案1】:

感谢您的投入。 我已经完成了我的问题的一部分,这需要我加密我的输入文本:“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

【讨论】:

    【解决方案2】:

    你不能用前导零来存储它们。前导零到整数表示它是八进制数。

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

    1. 您可以使用简单的以 10 为基数的数字来存储这些整数。我们将在需要时将它们转换为二进制文件。 (您也可以将它们简单地存储为带有前导 0b 的二进制文件。See this answer for more details.
    2. 使用Integer.toString(hm.get(s), 2); 显示二进制数。原始数字仍然是整数,因此您可以将其用于异或运算。
    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 +" ");
      }
      

    这是最终代码的样子:

    输出

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

    【讨论】:

    • 健康警告: XOR 结果会感觉完全随机(上面 cmets 中的 OP 表示下一步是对它们使用 XOR)。
    • @LukeBriggs 我的错。我没有看到那个评论。不过,感谢您的通知,我已经相应地更新了我的答案。
    【解决方案3】:

    首先,您的Map 声明和初始化有点偏离。要使用二进制常量,请在其前面加上 0b - 并请编程到 Map 接口(不是 HashMap 实现)。而且,从 Java 7 开始,您可以使用菱形运算符 &lt;&gt; 来缩短时间。

    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 
    

    【讨论】:

    • 非常感谢。这是准确的。为了确保我理解正确,我可以将二进制数以“0b000”...“0b100”等简单格式存储到 int 中。
    • @vishrutsharma 这就是您使用二进制文字的方式。所有数字在数字计算机中都是二进制的。
    • 在这种情况下,我不需要使用整个“Integer.toBinaryString”,因此可以直接显示我的哈希映射的内容(无需转换为字符串)。随后对内容执行 XOR,而无需执行任何转换。
    猜你喜欢
    • 2010-10-05
    • 2019-08-02
    • 2010-10-30
    • 2013-01-14
    • 2014-12-28
    • 2017-01-22
    • 2014-03-11
    • 2012-08-05
    • 2011-08-13
    相关资源
    最近更新 更多