ventajas tablas tabla resolucion metodo las implementacion funcion ejemplos dispersion desventajas colisiones busqueda aplicaciones java binary hashmap

resolucion - tablas hash en java



almacenar charcter y nĂºmero binario en un mapa hash (3)

Gracias por sus entradas chicos. He terminado una parte de mi problema, que me obligó a encriptar mi texto de entrada: "Heilhitler" y presentarlo en formato binario. Este fue el código que pude construir con sus sugerencias.

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

Salida:

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

Intento almacenar un mapeo de letras en un número binario. Aquí está mi asignación

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

Para este propósito, he creado un mapa hash y he almacenado los pares clave de valores. Ahora quiero mostrar el valor binario correspondiente para una oración dada. Aquí está mi código para el mismo.

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

Sin embargo, cuando ejecuto el código, el mapeo de la letra "i" se muestra incorrecto: 8 : en lugar de 010 . ¿Puede alguien decirme por qué sucede esto? Además, ¿cómo puedo mostrar los ceros delante de mis números, ya que estos son números binarios? Gracias.

Salida:

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


Primero, su declaración e inicialización de Map está un poco desajustada. Para usar constantes binarias, 0b con 0b y programe la interfaz de Map (no la implementación de HashMap ). Y, desde Java 7, puede usar el operador de diamante <> para acortar las cosas.

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

Luego, para imprimir, tiene Integer (s) pero desea su representación binaria. Entonces puedes hacer algo como,

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

Que corrí para obtener (como creo que esperabas)

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

Si realmente quieres los ceros iniciales (un formato binario de tres bits), podrías hacer

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

Que salidas

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


Usted simplemente no puede almacenarlos con cero a la izquierda. Llevar cero a un número entero indica que es un número octal.

Como su siguiente paso es XOR, recomiendo este enfoque.

  1. Puede almacenar estos enteros usando números simples de base 10. Los convertiremos cuando sea necesario como binario. ( También puede almacenarlos simplemente como binarios con el 0b líder. Consulte esta respuesta para obtener más detalles.
  2. Use Integer.toString(hm.get(s), 2); para mostrar el número binario El número original sigue siendo un Entero, por lo que puede usarlo para la operación XOR.
  3. Para mostrar el binario con el cero inicial, he jugado con algunos métodos de cadena como este:

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

Así es como se ve el código final:

Salida

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

Código

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