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

后端 未结 3 2011
名媛妹妹
名媛妹妹 2021-01-20 17:11

as the title says, how do I do it? Its easy to convert from string -> byte -> string binary, But how do I convert back? Below is a example. The output is : \'f\' to binary:

3条回答
  •  误落风尘
    2021-01-20 17:33

    You can use Byte.parseByte() with a radix of 2:

    byte b = Byte.parseByte(str, 2);
    

    Using your example:

    System.out.println(Byte.parseByte("01100110", 2));
    
    102
    

提交回复
热议问题