0

Is it possible to convert a byte array to a string but where the length of the string is exactly the same length as the number of bytes in the array? If I use the following:

byte[] data; // Fill it with data
data.toString();

The length of the string is different than the length of the array. I believe that this is because Java and/or Android takes some kind of default encoding into account. The values in the array can be negative as well. Theoretically it should be possible to convert any byte to some character. I guess I need to figure out how to specify an encoding that generates a fixed single byte width for each character.

EDIT:

I tried the following but it didn't work:

byte[] textArray; // Fill this with some text.
String textString = new String(textArray, "ASCII"); 
textArray = textString.getBytes("ASCII"); // textArray ends up with different data.
2
  • Convert to and from Base64 as shown in the following post. stackoverflow.com/a/7360440/753632 Commented Feb 5, 2013 at 15:50
  • yes why not use Base64? it's safer, because some bytes might not be represented properly using just String. Encode/Decode using Base64 is best practice -I think- Commented Dec 31, 2014 at 7:16

3 Answers 3

1

You can use the String constructor String(byte[] data) to create a string from the byte array. If you want to specify the charset as well, you can use String(byte[] data, Charset charset) constructor.

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

5 Comments

What characterset should I be using? Some generate fixed 2 bytes, some a variable width and some just one byte. I would like it to be a single byte. ASCII?
I presume you can use ASCII.
I edited my comment above to show some code I tried. It doesn't work. The values in the array before and after are different.
Apparently, the negative bytes are encoded differently. Why are you using String for this purpose?
Have no choice. Android forces you to use a String when saving data to SharePreferences. The data I have is in a byte array format. Solution is to encode using Base64. I put the link above.
0

Try your code sample with US-ASCII or ISO-8859-1 in place of ASCII. ASCII is not a built-in Character encoding for Java or Android, but one of those two are. They are guaranteed single-byte encodings, with a caveat that characters not in the character set will be silently truncated.

Comments

0

This should work fine!

public static byte[] stringToByteArray(String pStringValue){
    int length= pStringValue.length();
    byte[] bytes = new byte[length];

    for(int index=0; index<length; index++){
        char ch= pStringValue.charAt(index);
        bytes[index]= (byte)ch;
    }

    return bytes;
}

since JDK 1.6:

You can also use:

stringValue.getBytes() which will return you a byte array.

In case of passing a NULL string, you need to handle that by either throwing the nullPointerException or handling it inside the method itself.

Comments

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.