java


How to display , decrypted binary code to mappings in hashmap


I have a hash map that contains the following mappings ....
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);
Along with this i have a binary sequence , which i have obtained from another computation as
1 0 10 100 1 10 111 100 0 101
My objective is to get the resultant characters that these binary digits display from the hash map above .
For example .... 001 = 1 = h
000 = 0 = e
010 = 2 = i
This code is part of a program that implements the one time pad in cryptography .
I have performed the encryption as well as the decryption .
refer answer 3 here for the proof of code :
storing charcter and binary number in a hash map
But am struggling to display the decrypted binary code output , to the letters in my hash map .
Thanks in advance
You need to map from the value to the key (which is the reverse of the way a HashMap works). Build a decryption Map.
Map<Integer, String> dec = new HashMap<>();
for (Map.Entry<String, Integer> kp : hm.entrySet()) {
dec.put(kp.getValue(), kp.getKey());
}
Then you can iterate that by parsing your input values to int and getting the corresponding value. Like
String input = "1 0 10 100 1 10 111 100 0 101";
Stream.of(input.split("\\s+")).map(s -> dec.get(Integer.parseInt(s, 2)))
.forEachOrdered(s -> System.out.print(s + " "));
System.out.println();
Make a new map with the values and the rows swapped.
final Map<String, Integer> hm = ...
final Map<Integer, String> mapping = hm.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Now you can use this map to do your lookup:
final List<Byte> bytes = ...
final List<String> keys = bytes.stream()
.map(mapping::get)
.collect(Collectors.toList());

Related Links

Sqlite inserting doesn't work properly
My average of my ArrayList is incorrect
Create a java calculator
Ant build successful, yet Eclipse shows errors. (new to Eclipse / Java)
How do i split user input in java - and convert into double?
How to read data from file to create objects?
How do I import a AIDE project into IntelliJ?
hibernate not working on Jboss
Jtoolbar isn't displaying
java.util.ServiceLoader.load() function is useless and only returns empty result
Arraylist remove method except out of bound
Can't Mock Stored procedure in JOOQ
Ways to create custom commands for a text based adventure game?
implicit cast char array to int array
JNI thread causing SIGSEGV Cypress fx2lp USB chip
Alternative to using Graphics2D Area?

Categories

HOME
cakephp
sendgrid
image
azure-data-factory
plot
tesseract
gis
rubygems
win32gui
pivotal-cloud-foundry
echarts
opengl-es-2.0
convolution
networkx
azure-media-services
wheelnav.js
dax
modx-revolution
google-cloud-spanner
n-gram
percona
ghc
oxyplot
samoa
contextmenu
nodatime
dbext
grails3
visjs
crystal-reports-2010
facebook-apps
gsmcomm
stormpath
javascriptcore
fog
column-family
gammu
ghost4j
sequential
suricata
hot-module-replacement
swift3.0.2
android-browser
occlusion
ssjs
unobtrusive-validation
greenrobot-eventbus
fakeiteasy
zip4j
multipeer-connectivity
worksheet
windows-mobile-6.5
qtwebview
nbconvert
webdriverjs
blacklist
synchronous
crosswalk-runtime
time-and-attendance
color-picker
rvest
yaws
bluegiga
rhino-servicebus
ptrace
freelancer.com-api
freedesktop.org
tmuxinator
riak-cs
apache-commons-fileupload
composite
relocation
internet-connection
php-5.4
flask-cors
sgen
dml
funcunit
fluentautomation
wp-query
valueconverter
android-2.2-froyo
srs
file-locking
excel-2003
cos
xamlparseexception
sqlperformance
referrer
tinn-r
mt
rdoc
venn-diagram
amazon-appstore
ohm
netdna-api
joyent
servicehost
site.master
asp.net-mvc-areas
gamma
asp.net-profiles
vc90
firefox-5
multi-tier
iweb
yagni
ugc

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App