How to convert a binary representation of a string into byte in Java? -


as title says, how do it? easy convert string -> byte -> string binary, how convert back? below example. output : 'f' binary: 01100110 294984

i read somewhere use integer.parseint not case :( or doing wrong?

thanks, :)

public class main{     public static void main(string[] args) {           string s = "f";           byte[] bytes = s.getbytes();           stringbuilder binary = new stringbuilder();           (byte b : bytes)           {              int val = b;              (int = 0; < 8; i++)              {                 binary.append((val & 128) == 0 ? 0 : 1);                 val <<= 1;              }              binary.append(' ');           }           system.out.println("'" + s + "' binary: " + binary);          system.out.println(integer.parseint("01100110", 2));     } } 

you can use byte.parsebyte() radix of 2:

byte b = byte.parsebyte(str, 2); 

using example:

system.out.println(byte.parsebyte("01100110", 2)); 
 102 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -