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);
8
  • 1
    Check this one stackoverflow.com/questions/2057387/… Commented May 23, 2012 at 14:27
  • I'll edit it so it's a little more clear, but if you see in the code I have commments Commented May 23, 2012 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 :) Commented May 23, 2012 at 14:31
  • @StepTNT, that led me to the right answer. If you want to formalize the answer I'll accept it. Thanks. Commented May 23, 2012 at 14:33
  • 1
    There's no need for that. Just accept yours, I just gave you a link, no answer at all :) Commented May 23, 2012 at 15:36

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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)));

2 Comments

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.
I dont just want to copy the solution, but I added a link to it.
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));

Comments

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());

4 Comments

That is the same as new String(string.getBytes()). It does not print the binary representation of the String.
@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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.