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.
Peter Elliott
  • 3,273
  • 16
  • 30
Johann
  • 27,536
  • 39
  • 165
  • 279
  • Convert to and from Base64 as shown in the following post. http://stackoverflow.com/a/7360440/753632 – Johann Feb 05 '13 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- – Yazan Dec 31 '14 at 07:16

3 Answers3

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.

muhoweb
  • 116
  • 3
  • 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? – Johann Feb 05 '13 at 14:45
  • 1
    I presume you can use ASCII. – muhoweb Feb 05 '13 at 14:53
  • 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. – Johann Feb 05 '13 at 15:12
  • Apparently, the negative bytes are encoded differently. Why are you using String for this purpose? – muhoweb Feb 05 '13 at 15:26
  • 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. – Johann Feb 05 '13 at 15:52
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.

Peter Elliott
  • 3,273
  • 16
  • 30
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.

OnePunchMan
  • 720
  • 15
  • 33