开发者

Parsing a string of binary into text/characters

I am probably overlooking something silly, but I've never had to deal with binary in code and thought it'd be a good idea to practice it in an encryption program, for kicks. Long story short, I'm able to convert a string into binary (in the form of a string), but can't figure out how to do the reverse.

Right now, I have something like this:

public static String bytesToString(String bytes){
    int i = bytes.length()/8;
    int pos = 0;
    String result = "";
    for(int j=0; j<i; j++){
        String temp = bytes.substring(pos,pos+8);
        byte b = (byte) Integer.parseInt(temp);
        result = result + Byte.toString(b);
        pos++;
    }
    System.out.println("Result: " + result);
    return result;
}

I think the bytes are being parsed as literal numbers. What am I missing? Edit: To clarify, I will previously have parsed a string of text into bits and written them to a string. I want to sp开发者_C百科lit this string into bytes and parse them back into letters. It would take "011010000110010101111001" and return "hey".


How about using Integer.parseInt(text, 2)? As in,

public static int binaryToInt(String binary)
{
    return Integer.parseInt(binary, 2);
}

I'm not sure why your binaryToString method both takes and returns a string.


Integer.parseInt(temp) will attempt to read temp as a number and return the corresponding int. For example, Integer.parseInt("123") returns 123

EDIT: Be aware that the binary value of a character or text depends on the encoding you are using. For example "hi" is 0110100001101001 in ASCII but it may not in UTF-16 or UTF-32. And Java encodes characters into UTF-16 characters: see http://download.oracle.com/javase/6/docs/api/java/lang/String.html (for this reason Java chars are 16-bit unsigned integers).

So your bytesToString method must treat input differently depending on the encoding of the input. Or you may write it specifically for ASCII characters, and maybe rename it to, say, asciiBytesToString

You'd better see:

  • constructor String(byte[]) http://download.oracle.com/javase/6/docs/api/java/lang/String.html
  • Integer.parseInt(String s, int radix) http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html


public class BinaryStringToChars {
  public static void main(String[] args) {
    String bin = "011010000110010101111001";

    StringBuilder b = new StringBuilder();
    int len = bin.length();
    int i = 0;
    while (i + 8 <= len) {
      char c = convert(bin.substring(i, i+8));
      i+=8;
      b.append(c);
    }
    System.out.println(b.toString());
  }

  private static char convert(String bs) {
    return (char)Integer.parseInt(bs, 2);
  }
}


You need to advance 8 digits at a time, not digit by digit. Otherwise you are reusing bits. Also, you need to tell Integer.parseInt() what radix you want to use, since parseInt(String val) cannot really detect binary (you want Integer.parseInt(String val, int radix). You also need to choose a character encoding to convert bytes into characters (they are not the same thing!). Assuming ISO-8859-1 is ok:

public static String bytesToString(String bytes){
    int i = bytes.length()/8;
    int pos = 0;
    String result = "";
    byte[] buffer = new byte[i];
    for(int j=0; j<i; j++){
        String temp = bytes.substring(pos,pos+8);
        buffer[j] = (byte) Integer.parseInt(temp, 2);
        pos+=8;
    }
    result = new String(buffer, "ISO-8859-1");
    System.out.println("Result: " + result);
    return result;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜