In some cases, we have to convert String variable into a Byte array format, for example, fetching the scan result of sprind-data redis connection.
Exp
org.springframework.data.redis.core.Cursor c = redisConnection.scan(scanOption);
However how do we convert a Byte[] array to String ?
Simple toString() function like following code will not work. It will not give the original text but byte value.
Exp
org.springframework.data.redis.core.Cursor
However how do we convert a Byte[] array to String ?
Simple toString() function like following code will not work. It will not give the original text but byte value.
String s = bytes.toString();
In
order to convert Byte array into String format correctly, we have to
explicitly create a String object and assign the Byte array to it.String s = new String(bytes);
public class TestByte
{
public static void main(String[] argv) {
String example = "This is a string";
byte[] bytes = example.getBytes();
System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());
String s = new String(bytes);
System.out.println("Text decrypted : " + s);
}
}
Output
Text : This is a string
Text [Byte Format] : [B@187aeca
Text [Byte Format] : [B@187aeca
Text decrypted : This is a string