Convert a String to a byte array and then back to the original String

JavaAndroidByteHashArduino

Java Problem Overview


Is it possible to convert a string to a byte array and then convert it back to the original string in Java or Android?

My objective is to send some strings to a microcontroller (Arduino) and store it into EEPROM (which is the only 1  KB). I tried to use an MD5 hash, but it seems it's only one-way encryption. What can I do to deal with this issue?

Java Solutions


Solution 1 - Java

I would suggest using the members of string, but with an explicit encoding:

byte[] bytes = text.getBytes("UTF-8");
String text = new String(bytes, "UTF-8");

By using an explicit encoding (and one which supports all of Unicode) you avoid the problems of just calling text.getBytes() etc:

  • You're explicitly using a specific encoding, so you know which encoding to use later, rather than relying on the platform default.
  • You know it will support all of Unicode (as opposed to, say, ISO-Latin-1).

EDIT: Even though UTF-8 is the default encoding on Android, I'd definitely be explicit about this. For example, this question only says "in Java or Android" - so it's entirely possible that the code will end up being used on other platforms.

Basically given that the normal Java platform can have different default encodings, I think it's best to be absolutely explicit. I've seen way too many people using the default encoding and losing data to take that risk.

EDIT: In my haste I forgot to mention that you don't have to use the encoding's name - you can use a Charset instead. Using Guava I'd really use:

byte[] bytes = text.getBytes(Charsets.UTF_8);
String text = new String(bytes, Charsets.UTF_8);

Solution 2 - Java

You can do it like this.

String to byte array

String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";
byte[] theByteArray = stringToConvert.getBytes();

http://www.javadb.com/convert-string-to-byte-array

Byte array to String

byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
String value = new String(byteArray);

http://www.javadb.com/convert-byte-array-to-string

Solution 3 - Java

Use [String.getBytes()][1] to convert to bytes and use [String(byte[] data)][2] constructor to convert back to string.

Solution 4 - Java

import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;

public class FileHashStream	
{
	// write a new method that will provide a new Byte array, and where this generally reads from an input stream
	
	public static byte[] read(InputStream is) throws Exception
	{
		String path = /* type in the absolute path for the 'commons-codec-1.10-bin.zip' */;

		// must need a Byte buffer

		byte[] buf = new byte[1024 * 16]

		// we will use 16 kilobytes

		int len = 0;

		// we need a new input stream

		FileInputStream is = new FileInputStream(path);

		// use the buffer to update our "MessageDigest" instance

		while(true)
		{
			len = is.read(buf);
			if(len < 0) break;
			md.update(buf, 0, len);
		}

		// close the input stream

		is.close();

		// call the "digest" method for obtaining the final hash-result

		byte[] ret = md.digest();

		System.out.println("Length of Hash: " + ret.length);

		for(byte b : ret)
		{
			System.out.println(b + ", ");
		}

		String compare = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";

		String verification = Hex.encodeHexString(ret);

		System.out.println();

		System.out.println("===")

		System.out.println(verification);

		System.out.println("Equals? " + verification.equals(compare));
			
	}
}

Solution 5 - Java

byte[] pdfBytes = Base64.decode(myPdfBase64String, Base64.DEFAULT)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEng.FouadView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaMichell BakView Answer on Stackoverflow
Solution 3 - JavaSzere DyeriView Answer on Stackoverflow
Solution 4 - Javauser6798393View Answer on Stackoverflow
Solution 5 - JavaMarcoView Answer on Stackoverflow