开发者

how to convert string to byte in java?

How to make string "01001000"(for example) to byte and convert it to string.

Example :

if string = "0110000101100010" then output must be "ab"

because开发者_如何学运维 a == 01100001 and b == 01100010


something like this:

      String[] array = {"01100001","01100010"};
      StringBuilder sb = new StringBuilder();
      for( String string : array ) {
          sb.append( (char)Integer.parseInt( string, 2 ) );
      }

or if you have one String which has exact 8-bit * x letter.

    String source = "0110000101100010";
    StringBuilder sb = new StringBuilder();
    for( int i = 0; i < source.length(); i= i+8 ) {
        sb.append( (char)Integer.parseInt( source.substring( i, i+8 ), 2 ) );
    }


StringBuilder sb = new StringBuilder();
for(String str : "0110000101100010".split("(?<=\\G.{8})")){
    sb.append((char)Byte.parseByte(str,2));
}
System.out.println(sb.toString());

Will output --> ab

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜