将charcter和二进制数存储在散列图中 [英] storing charcter and binary number in a hash map

查看:116
本文介绍了将charcter和二进制数存储在散列图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储一个字母到二进制数字的映射。这里是我的映射

$ $ $ $ $ $ $ $ $ $ $ $ (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 = trsrtlerse
String [] input = {h,e,i,l,h,i,t,l , E, R};
int [] cipher = new int [10];
System.out.println(文本的二进制形式是...);

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

}

}



<但是,当我运行代码时,字母i的映射显示为错误: 8 :而不是 010
有人可以告诉我为什么会发生这种情况吗?另外我怎样才能显示我的号码前面的零,因为这些是二进制数字。
Thanks。



输出:

 二进制形式text is .... 
1 0 8 100 1 8 111 100 0 101


解决方案

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



由于您的下一步是XOR,我建议您采用这种方法。


  1. 您可以使用简单的10位数字来存储这些整数。我们会在需要时将它们转换为二进制。 (),您也可以将它们简单地保存为二进制文件,其中 0b 请参阅
  2. 使用 Integer.toString(hm.get(s),2); 显示二进制数字,原始数字仍然是一个整数,因此您可以将其用于XOR操作。
  3. 为了显示带前导零的二进制数,我玩过一些像这样的字符串方法:

    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $二进制
    for(String s:input) {
    binary = Integer.toString(hm.get(s),2);
    System.out.print(temp.substring(0,3-binary.length())+ binary + );
    }


最终的代码如下所示:

输出

 文本的二进制形式是... 
001 000 010 100 001 010 111 100 000 101

代码

  import java.ut IL *。 

public class onetimepad {
public static void main(String args []){
HashMap< String,Integer> hm = new HashMap< String,Integer> ();
hm.put(e,0); //或使用hm.put(e,0b000);
hm.put(h,1); //或使用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 = trsrtlerse
String [] input = {h,e,i,l,h,i,t,l , E, R};
int [] cipher = new int [10];
System.out.println(文本的二进制形式是...);

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


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 

解决方案

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.

  1. 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.
  2. 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.
  3. 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 +" ");
        }
    }   
}

这篇关于将charcter和二进制数存储在散列图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆