0

The question is in comments in the code, I thought that'd be an easier way to ask...

Easy question, but I can't seem to find an answer. I want to convert a String to it's byte[] (easy, String.getBytes()). Then I want to convert a String of bytes (101011010101001 for example) to a byte[] and get the String value of that (that's easy too: new String(byte[]))

Here's what I've got so far:

Scanner scan = new Scanner(System.in);
String string = scan.nextLine();
String byteString = "";
for (byte b : string.getBytes()) {
  byteString += b;
}
System.out.println(byteString);

//This isn't exactly how it works, these two parts in separate methods, but you get the idea...

String byteString = scan.nextLine();
byte[] bytes = byteString.literalToBytes() //<== or something like that...
//The line above is pretty much all I need...
String string = new String(bytes);
System.out.println(string);
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
  • 1
    Check this one http://stackoverflow.com/questions/2057387/converting-a-set-of-strings-to-a-byte-array – StepTNT May 23 '12 at 14:27
  • I'll edit it so it's a little more clear, but if you see in the code I have commments – kentcdodds May 23 '12 at 14:27
  • 1
    First of all I would suggest you to use a StringBUilder for the variable byteString and then convert it back to string after the loop. Little Optimization :) – dharam May 23 '12 at 14:31
  • @StepTNT, that led me to the right answer. If you want to formalize the answer I'll accept it. Thanks. – kentcdodds May 23 '12 at 14:33
  • @dharam, duly noted and fixed. Thanks for the tip. – kentcdodds May 23 '12 at 14:39
  • I see that you added your own solution, you can accept that one :) – StepTNT May 23 '12 at 15:05
  • @StepTNT I tried to give you as much credit as I could. If you'd like I can remove mine and you can add one so you get the credit. Either way, thanks a lot. – kentcdodds May 23 '12 at 15:15
  • 1
    There's no need for that. Just accept yours, I just gave you a link, no answer at all :) – StepTNT May 23 '12 at 15:36

4 Answers4

1

EDIT

Please see this answer for a solution.

With this you can:

String string = scan.nextLine();
String convertByte = convertByte(string.getBytes());
System.out.println(convertByte);
String byteString = scan.nextLine();
System.out.println(new String(convertStr(byteString)));
Community
  • 1
  • 1
morja
  • 8,297
  • 2
  • 39
  • 59
  • Sorry, that wont work for the cases I'm working with. I just tried it with the string `kent` which gave me a byte string of: `107101110116`. Giving your answer that byte string throws a NumberFormatException on `Integer.toBinaryString(Integer.parseInt(string));` Which makes sense. But one of the commenters on my question gave me a link to a good answer. If they doesn't post the comment in an answer I'll just post my answer. Thanks for the help though. – kentcdodds May 23 '12 at 14:47
  • I dont just want to copy the solution, but I added a link to it. – morja May 23 '12 at 14:56
1

This won't work. The problem is that when you convert your bytes to a string you are going to get a string like

2532611134

So analyzing this string, is the first byte 2, or 25, or 253?

The only way to make this work would be to use a DecimalFormat and make sure every byte is 3 characters long in your string

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Another option would be to convert to hexadecimal (2 digits 0-F per byte) or use a separator between the bytes, e.g. space character. – user1252434 May 23 '12 at 15:13
1

Alright, because the commenter who pointed me to this question (which lead me to this answer) isn't going to answer, I'll just post the solution here:

Scanner scan = new Scanner(System.in);
String pass = scan.nextLine();
StringBuilder byteString = new StringBuilder();
for (byte b : pass.getBytes()) {
  b = (byte) (b);
  byteString.append(b).append(","); //appending that comma is what does the trick.
}
System.out.println(byteString);
//
String[] split = byteString.toString().split(","); //splitting by that comma is what does the trick... too...
byte[] bytes = new byte[split.length];
for (int i = 0; i < split.length; i++) {
  bytes[i] = (byte) (Byte.valueOf(split[i]).byteValue());
}
System.out.println(new String(bytes));
Community
  • 1
  • 1
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
0

I guess what you want is this

// to get back the string from byte array
StringBuilder byteString = new StringBuilder();
for (byte b : string.getBytes()) {
    byteString.append((char)b);
}
   System.out.println(byteString.toString());
// to get the binary representation from string
StringBuilder byteString = new StringBuilder();
    for (byte b : string.getBytes()) {
        System.out.print(Integer.toBinaryString((int)b));
    }
        System.out.println(byteString.toString());
dharam
  • 7,882
  • 15
  • 65
  • 93
  • That is the same as new String(string.getBytes()). It does not print the binary representation of the String. – morja May 23 '12 at 14:49
  • @morja, that's right. I need it to print the byte representation and then convert the string version of the byte representation back to the string it represented originally. Someone answered the question in a comment though. I'm just waiting for a formalized answer so I can accept it. – kentcdodds May 23 '12 at 14:50
  • Awesome, now to answer the *original* question. How to convert that byte string back to the original string it represents? I already got the answer, but feel free to update your answer if you like. – kentcdodds May 23 '12 at 14:55
  • You have to substring the byteString into pieces of length 8 and individually parse each of them into int and then typecaste into a byte and then typecaset to char. – dharam May 23 '12 at 15:12