Storing charcter and binary in a hash map

I am trying to preserve 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)

To this end, I created a hash map and saved key value pairs. Now I want to display the corresponding binary value for this 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)+" ");
    }

}   

}

However, when I run the code, the display for the letter "i" does not display correctly 8:: instead 010. Can someone tell me why this is happening? Also how can I display a zero infront of my numbers since they are binary numbers. Thank.

Output:

Binary form of text is ....
1 0 8 100 1 8 111 100 0 101 
+4
source share
3 answers

. , .

- XOR, .

  • , 10 . , , . ( 0b. . .
  • Integer.toString(hm.get(s), 2); . - , XOR.
  • , :

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

Thanks for your guys. I finished one part of my problem, which required me to encrypt my input text: "Heilhitler" and present it in binary format. This was the code that 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
0
source

Source: https://habr.com/ru/post/1668346/


All Articles