How to parse String as Binary and convert it to UTF-8 equivalent in Java? -


i need parse string content binary sequence , convert them utf-8 equivalent string.

for example, utf-8 binary equivalents of b, a , r follows:
b = 01000010
a = 01000001
r = 01010010


now, need convert string "010000100100000101010010" string "bar"
i.e. above case input string 24 characters divided 3 equal parts(8 character in each part) , translated utf-8 equivalent string value.

sample code:

public static void main(string args[]) {     string b = "01000010";     string = "01000001";     string r = "01010010";     string bar = "010000100100000101010010";      string utfequiv = toutf8(bar);//expecting "bar"     system.out.println(utfequiv); }  private static string toutf8(string str) {     // todo      return ""; } 

what should implementation of method toutf8(string str){}

you should separate 2 problems:

  • converting string byte array parsing binary values
  • converting byte array string using utf-8

the latter straightforward, using new string(bytes, standardcharsets.utf_8).

for first part, tricky part byte.parsebyte won't automatically handle leading 1... i'd parse each 8-bit string short , cast byte:

public static byte[] binarytobytes(string input) {     // todo: argument validation (nullity, length)     byte[] ret = new byte[input.length() / 8];     (int = 0; < ret.length; i++) {         string chunk = input.substring(i * 8, * 8 + 8);         ret[i] = (byte) short.parseshort(chunk, 2);     }     return ret; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -