java - How to turn Binary String into text? -
i working on personal project. want create encryption program lets encrypt, , decrypt string using key. finished need final part. want convert binary string text. let's binary outcome(which want convert normal text) is:
01001000011000010110100001100001
this converted text "haha".
note: working bigintegers since every number using big normal integer.
edit: found answer using this code:
stringbuffer output = new stringbuffer(); (int = 0;i < input.length();i += 8) { output.append((char) integer.parseint(input.substring(i, + 8), 2)); } system.out.println(output);
this code in 1 line:
string str = "01001000011000010110100001100001"; string decoded = arrays.stream(str.split("(?<=\\g.{8})")) .map(s -> integer.parseint(s, 2)) .map(i -> "" + character.valueof((char)i.intvalue())) .collect(collectors.joining("")); // "haha"
it can made prettier, didn't have ide handy (just thumbed in on phone).
you'll note biginteger
isn't needed, , code handles input of arbitrarily large length.
Comments
Post a Comment