Convert Byte[] to String and Vice-versa
Learn to convert byte[] array to String and convert String to byte[] array in Java with examples. Conversion between byte array and string may be used in many cases including IO operations, generating secure hashes etc.
Until it is absolute necessary, DO NOT convert between string and byte array. They both represent different data; and are there to serve specific purposes i.e. strings are for text, byte[] is for binary data.
1.1. Using String Constructor
To convert a byte array to String , you can use String class constructor with byte[] as the constructor argument.
byte[] bytes = "hello world".getBytes(); String s = new String(bytes);
Since Java 8, we have Base64 class available. As you might be aware that Base64 is a way to encode binary data, while UTF-8 and UTF-16 are ways to encode Unicode text data. So if you need to encode arbitrary binary data as text, Base64 is the way to go.
byte[] bytes = "hello world".getBytes(); String s = Base64.getEncoder().encodeToString(bytes);
To convert from string to byte array, use String.getBytes() method. Please note that this method uses the platform’s default charset.
String string = "howtodoinjava.com"; byte[] bytes = string.getBytes();
The Base64.getDecoder().decode() method converts a string to a byte array.
String string = "howtodoinjava.com"; byte[] bytes = Base64.getDecoder().decode(string);
We should focus on the input data type when converting between byte[] array and String in Java.
- Use the String class when you input data in string or text content.
- Use Base64 class when you input data in a byte array.
Drop me your questions in the comments section.
How to convert array of byte to String in Java? [duplicate]
bytes and bytes2 are different. PS: UTF-8 Does not work because it convert some bytes in different values. I tested and it does not work. PS2: And no, I don’t want BASE64 .
@TheNewIdiot the answer in that post solve nothing. I wish for a byte to byte conversion and the answer say «convert it or bust». How is it possible that Java can’t do that?.
Java makes a superb distinction between binary data (bytes) and text (String). For text they chose internally Unicode, so all languages are covered. Though you can use an encoding like ISO-8559-1 to convert bytes as they are to a String and vice versa, these Strings may have artifacts like a binary 0.
You almost certainly do want Base64, which is the only way you’re going to get reversible byte-to-String encoding.
3 Answers 3
You need to specify the encoding you want e.g. for UTF-8
String doc = . byte[] bytes = doc.getBytes("UTF-8"); String doc2 = new String(bytes, "UTF-8");
doc and doc2 will be the same.
To decode a byte[] you need to know what encoding was used to be sure it will decode correctly.
Here’s one way to convert an array of bytes into a String and back:
String doc=new String(bytes, "ISO-8859-1"); byte[] bytes2=doc.getBytes("ISO-8859-1");
A String is a sequence of characters, so you’ll have to somehow encode bytes as characters. The ISO-8859-1 encoding maps a single, unique character for each byte, so it’s safe to use it for the conversion. Note that other encodings, such as UTF-8 , are not safe in this sense because there are sequences of bytes that don’t map to valid strings in those encodings.
Convert byte to string in Java
Note especially that converting bytes to Strings always involves an encoding. If you do not specify it, you’ll be using the platform default encoding, which means the code can break when running in different environments.
To avoid the checked UnsupportedEncodingException thrown by that constructor you’d probably want to pass in StandardCharsets.US_ASCII instead of using the charset name.
The string ctor is suitable for this conversion:
System.out.println("string " + new String(new byte[] ));
Because gustafc’s answer has a very important point: String(byte[]) constructor uses the System default encoding to convert the byte array into String characters. One should not assume that a 0x63 byte value is mapped to the letter ‘c’. For example, in UTF-16 the letter ‘c’ is represented by 2 encoding bytes, not one. This blog post elaborates on this: joelonsoftware.com/articles/Unicode.html
System.out.println("string " + (char)0x63);
Or if you want to be a Unicode puritan, you use codepoints:
System.out.println("string " + new String(new int[]< 0x63 >, 0, 1));
And if you like the old skool US-ASCII «every byte is a character» idea:
System.out.println("string " + new String(new byte[]< (byte)0x63 >, StandardCharsets.US_ASCII));
Avoid using the String(byte[]) constructor recommended in other answers; it relies on the default charset. Circumstances could arise where 0x63 actually isn’t the character c.
System.out.printf("string %c\n", 0x63);
You can as well create a String with such formatting, using String#format :
String s = String.format("string %c", 0x63);
the character equivalent to 0x63 is ‘c’ but byte equivalent to it is 99
System.out.println("byte "+(char)0x63);
You have to construct a new string out of a byte array. The first element in your byteArray should be 0x63 . If you want to add any more letters, make the byteArray longer and add them to the next indices.
byte[] byteArray = new byte[1]; byteArray[0] = 0x63; try < System.out.println("string " + new String(byteArray, "US-ASCII")); >catch (UnsupportedEncodingException e) < // TODO: Handle exception. e.printStackTrace(); >
Note that specifying the encoding will eventually throw an UnsupportedEncodingException and you must handle that accordingly.
How to convert byte array to string and vice versa?
I have to convert a byte array to string in Android, but my byte array contains negative values. If I convert that string again to byte array, values I am getting are different from original byte array values. What can I do to get proper conversion? Code I am using to do the conversion is as follows:
// Code to convert byte arr to str: byte[] by_original = ; String str1 = new String(by_original); System.out.println("str1 >> "+str1); // Code to convert str to byte arr: byte[] by_new = str1.getBytes(); for(int i=0;i> "+str1);
Why are you trying to convert arbitrary binary data to a String in the first place? Apart from all the charset problems the answers already mention, there’s also the fact that you’re abusing String if you do this. What’s wrong with using a byte[] for your binary data and String for your text?
@Joachim — sometimes you have external tools that can do things like store strings. You want to be able to turn a byte array into a (encoded in some way) string in that case.
25 Answers 25
Your byte array must have some encoding. The encoding cannot be ASCII if you’ve got negative values. Once you figure that out, you can convert a set of bytes to a String using:
byte[] bytes = String str = new String(bytes, StandardCharsets.UTF_8); // for UTF-8 encoding
There are a bunch of encodings you can use, look at the supported encodings in the Oracle javadocs.
@UnKnown because UTF-8 encodes some characters as 2- or 3- byte strings. Not every byte array is a valid UTF-8-encoded string. ISO-8859-1 would be a better choise: here each character is encoded as a byte.
to map one byte to one char (with 8859-1) and no exception handling (with nio.charset): String str = new String(bytes, java.nio.charset.StandardCharsets.ISO_8859_1);
The «proper conversion» between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no «proper conversion». String s are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.
If you really must use a String to hold binary data then the safest way is to use Base64 encoding.
The root problem is (I think) that you are unwittingly using a character set for which:
in some cases. UTF-8 is an example of such a character set. Specifically, certain sequences of bytes are not valid encodings in UTF-8. If the UTF-8 decoder encounters one of these sequences, it is liable to discard the offending bytes or decode them as the Unicode codepoint for «no such character». Naturally, when you then try to encode the characters as bytes the result will be different.
- Be explicit about the character encoding you are using; i.e. use a String constructor and String.toByteArray method with an explicit charset.
- Use the right character set for your byte data . or alternatively one (such as «Latin-1» where all byte sequences map to valid Unicode characters.
- If your bytes are (really) binary data and you want to be able to transmit / receive them over a «text based» channel, use something like Base64 encoding . which is designed for this purpose.
For Java, the most common character sets are in java.nio.charset.StandardCharsets . If you are encoding a string that can contain any Unicode character value then UTF-8 encoding ( UTF_8 ) is recommended.
If you want a 1:1 mapping in Java then you can use ISO Latin Alphabet No. 1 — more commonly just called «Latin 1» or simply «Latin» ( ISO_8859_1 ). Note that Latin-1 in Java is the IANA version of Latin-1 which assigns characters to all possible 256 values including control blocks C0 and C1. These are not printable: you won’t see them in any output.
From Java 8 onwards Java contains java.util.Base64 for Base64 encoding / decoding. For URL-safe encoding you may want to to use Base64.getUrlEncoder instead of the standard encoder. This class is also present in Android since Android Oreo (8), API level 26.
String to byte array, byte array to String in Java
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Today we will learn how to convert String to byte array in java. We will also learn how to convert byte array to String in Java.
String to byte array
We can use String class getBytes() method to encode the string into a sequence of bytes using the platform’s default charset. This method is overloaded and we can also pass Charset as argument. Here is a simple program showing how to convert String to byte array in java.
package com.journaldev.util; import java.util.Arrays; public class StringToByteArray < public static void main(String[] args) < String str = "PANKAJ"; byte[] byteArr = str.getBytes(); // print the byte[] elements System.out.println("String to byte array: " + Arrays.toString(byteArr)); >>
Below image shows the output when we run the above program. We can also get the byte array using the below code.
byte[] byteArr = str.getBytes("UTF-8");
However if we provide Charset name, then we will have to either catch UnsupportedEncodingException exception or throw it. Better approach is to use StandardCharsets class introduced in Java 1.7 as shown below.
byte[] byteArr = str.getBytes(StandardCharsets.UTF_8);
Java byte array to String
package com.journaldev.util; public class ByteArrayToString < public static void main(String[] args) < byte[] byteArray = < 'P', 'A', 'N', 'K', 'A', 'J' >; byte[] byteArray1 = < 80, 65, 78, 75, 65, 74 >; String str = new String(byteArray); String str1 = new String(byteArray1); System.out.println(str); System.out.println(str1); > >
Below image shows the output produced by the above program. Did you notice that I am providing char while creating the byte array? It works because of autoboxing and char ‘P’ is being converted to 80 in the byte array. That’s why the output is the same for both the byte array to string conversion. String also has a constructor where we can provide byte array and Charset as an argument. So below code can also be used to convert byte array to String in Java.
String str = new String(byteArray, StandardCharsets.UTF_8);
byte[] byteArray1 = < 80, 65, 78, 75, 65, 74 >; String str = new String(byteArray1, 0, 3, StandardCharsets.UTF_8);
Above code is perfectly fine and ‘str’ value will be ‘PAN’. That’s all about converting byte array to String in Java. You can checkout more array examples from our GitHub Repository. Reference: getBytes API Doc
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us