In Java, how to convert correctly byte[] to String to byte[] again?

string to byte array java
java byte array
java byte array to string utf-8
java byte array to hex string
add string to byte array java
convert string to byte python
byte array to string c#
convert byte to string java android

I want to convert the result of a TEA encryption (a byte[]) to a String and then convert it again to a byte[] and retrieve the same byte[].

//Encryption in the sending side
String stringToEncrypt = "blablabla"
byte[] encryptedDataSent = tea.encrypt(stringToEncrypt.getBytes());
String dataToSend = new BigInteger(encryptedDataSent).toString());

//Decryption side in the reception side
byte[] encryptedDataReceived = new BigInteger(dataToSend).toByteArray();

However, when I try this :

System.out.println(new String(encryptedDataSent));

System.out.println(new String(encryptedDataReceived));

boolean equality = Arrays.equals(encryptedDataReceived,encryptedDataSent);
System.out.println("Are two byte arrays equal ? : " + equality);

The output is :



Are two byte arrays equal ? : false

So, it looks like the two byte[] are the same when we print it but they are not exactly the same as we see "false" and this is a problem for the decryption that I perform after that.

I also tried to send a String with new String(byte[]) but it has the same problem when we want to convert it back to a byte[]

I want to have exactly the same byte[] in the beginning and after the conversion byte[]->String->byte[]

Do you have a solution or understand what I do wrong in my conversion ?

Don't try to convert from the byte[] to String as if it were regular encoded text data - it isn't. It's an arbitrary byte array.

The simplest approaches are to convert it to base64 or hex - that will result in ASCII text which can be reversibly decoded back to the same binary data. For example, using a public domain base64 encoder:

String dataToSend = Base64.encodeBytes(encryptedDataSent);
byte[] encryptedDataReceived = Base64.decode(receivedText);

String to byte array, byte array to String in Java, 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. I want to convert the result of a TEA encryption (a byte[]) to a String and then convert it again to a byte[] and retrieve the same byte[]. //Encryption in the sending side String stringToEncrypt = "

Try to use in the decryption byte[] encode = Base64.encode(bytesToStore, Base64.DEFAULT)

3 ways to convert String to byte array in Java, Suppose you have a String "abcd" and you want to convert it into a byte array, how will you do String to byte array in Java, but unfortunately, many developers don't use it correctly. Here is an example of converting String to byte[] in Java : This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String enc) method, which takes an encoding-name argument, or the toString() method, which uses the platform's default character encoding.

You can't. String is not a container for binary data. It is a container for UTF-16 characters. The round trip between chars and bytes is nowhere guaranteed.

How to convert Byte[] array to String in Java, In Java, we can use new String(bytes, StandardCharsets. toString()); // convert bytes[] to string String s = new String(bytes, StandardCharsets. toString is working properly if you see the documentation you will know. containing strings , convert it to a readable string, and then deserialize it back to map. Convert string to integer. Converting string to int type value can be done through the following methods. static int parseInt(String s) parses the string argument as a signed decimal integer. static int parseInt(String s, int radix) parses the string argument in the radix specified by the second argument.

try to specify charset explicitly. UTF-8 is ok for major cases:

public static void main(String[] args) {
    String in = "幸福";
    try {
        byte[] bytes = in.getBytes("utf-8");
        String out = new String(bytes, "utf-8");
        System.out.println(in + " -> " + out);
        System.out.println("equals: " + out.equals(in));
    } catch (UnsupportedEncodingException unsupportedEncodingException) {
        // do something

Please note that you will get exactly the same result while you byte array remains unchanged.

Convert byte[] Array to String in Java, Learn to convert byte[] array to String and convert String to byte[] array in Java with examples. Conversion between byte array and string may� I want to convert from String to Byte whereas the resulted Array equal to my first Important note: I do not know the Without knowing the code page that used to get the original array myArray i want that: convert from Byte array to string then to Byte array again Not from string to Byte array then to string

Convert a Byte Array to String in Java, We know that a String stores textual data in Java, and byte[] stores binary data, so conversion between the two should be avoided. Nevertheless, we might find� As I promessed I will tell u how to do it INTRO-----There is direct way that I know of for converting a byte into a String using the library classes and archives that come when U donwload java. Now via surfing the internet I came up accross a site that offered a class named "ByteVariable" that has can be used for converting a byte into may data

Convert string to byte array c, Result: The string[] required about 480,000 bytes. e convert string to hex array c ++. How to convert Java Array to JSON array?hex string to byte array, C. New in GetBytes(someString); You will need to turn it back into a string like this: string I did not correct this, because i wanted to provide example how to convert list of� This is different from converting a string of hex digits for which you can use int(xxx, 16), but instead I want to convert a string of actual byte values. UPDATE: I kind of like James' answer a little better because it doesn't require importing another module, but Greg's method is faster:

How to Convert byte Array to String in Java, The process of converting a byte array to a String is called decoding. convert a byte array into String, we can use String class constructor with byte[] as the When we use a different character encoding, we do not get the original string back. such cases our platform is not able to convert those special characters properly. 1) For the developer the symetry of being able to convert from a byte array to Cp1252 and back to the same byte array is both logical and convienient. 2) A Java String is a data type and therefore simply contains data to be manipulated and should not make any assumptions about display.

  • check out these two links, it may be helpful: string to byte and vice versa, another link
  • Thank you for your answer. But if my need is to write the byte[] data into a text file and then to be able to copy the text, paste it in a field and retrieve the data in what format should I store the byte[] data in the text file ?
  • @RédaBk: I'd use base64 for that, just as I've shown.
  • Thank you @JonSkeet it worked ! The Base64 encoding gave me a String like this (AAAAKSvMGMmVa75daqF+XuFyDdnbDrhljLsv8o+fl7ZOfE5rdEOVv5EIxrQBGy2FgQVVwQ==) and then I was able to convert this to the original byte[] again ! Why does this String encoding works for proper byte conversion and not others ? Wondering...
  • You can't do it like that, but the idea that you can't represent arbitrary binary data as text and then convert it back to the same binary data is clearly flawed... hex, base64, base32... any number of ways exist.
  • No, UTF-8 is not okay when the data is arbitrary binary data, rather than UTF-8-encoded text. The OP wants to convert the result of encryption as text. That is not UTF-8-encoded text.