BAEL-2313 Convert String to Byte Array and Reverse in Java

This commit is contained in:
chandra 2018-11-09 21:08:02 -05:00
parent d578e45a8f
commit be17633174
2 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,91 @@
package com.baeldung.string.conversion;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public class ByteArrayToStringUnitTest {
@Test
public void whenStringConstructorWithDefaultCharset_thenOK() {
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
108, 100, 33 };
String string = new String(byteArrray);
System.out.println(string);
assertNotNull(string);
}
@Test
public void whenStringConstructorWithNamedCharset_thenOK()
throws UnsupportedEncodingException {
final String charsetName = "IBM01140";
final byte[] byteArrray = { -56, -123, -109, -109, -106, 64, -26, -106,
-103, -109, -124, 90 };
String string = new String(byteArrray, charsetName);
assertEquals("Hello World!", string);
}
@Test
public void whenStringConstructorWithCharSet_thenOK() {
final Charset charset = Charset.forName("UTF-8");
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
108, 100, 33 };
String string = new String(byteArrray, charset);
assertEquals("Hello World!", string);
}
@Test
public void whenStringConstructorWithStandardCharSet_thenOK() {
final Charset charset = StandardCharsets.UTF_16;
final byte[] byteArrray = { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 };
String string = new String(byteArrray, charset);
assertEquals("Hello World!", string);
}
@Test
public void whenDecodeWithCharset_thenOK() {
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108,
100, 33 };
final Charset charset = StandardCharsets.UTF_8;
String string = charset.decode(ByteBuffer.wrap(byteArrray)).toString();
System.out.println(string);
assertEquals("Hello World!", string);
}
@Test
public void whenUsingCharsetDecoder_thenOK()
throws CharacterCodingException {
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108,
100, 33 };
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?");
String string = decoder.decode(ByteBuffer.wrap(byteArrray)).toString();
assertEquals("Hello World!", string);
}
}

View File

@ -0,0 +1,119 @@
package com.baeldung.string.conversion;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.UnsupportedEncodingException;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Test;
public class StringToByteArrayUnitTest {
@Test
public void whenGetBytesWithDefaultCharset_thenOK() {
final String inputString = "Hello World!";
final String defaultCharSet = Charset.defaultCharset().displayName();
byte[] byteArrray = inputString.getBytes();
System.out.printf(
"Using default charset:%s, Input String:%s, Output byte array:%s\n",
defaultCharSet, inputString, Arrays.toString(byteArrray));
assertNotNull(byteArrray);
assert (byteArrray.length >= inputString.length());
}
@Test
public void whenGetBytesWithNamedCharset_thenOK()
throws UnsupportedEncodingException {
final String inputString = "Hello World!";
final String charsetName = "IBM01140";
byte[] byteArrray = inputString.getBytes("IBM01140");
System.out.printf(
"Using named charset:%s, Input String:%s, Output byte array:%s\n",
charsetName, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { -56, -123, -109, -109, -106, 64, -26,
-106, -103, -109, -124, 90 },
byteArrray);
}
@Test
public void whenGetBytesWithCharset_thenOK() {
final String inputString = "Hello World!";
final Charset charset = Charset.forName("UTF-8");
byte[] byteArrray = inputString.getBytes(charset);
System.out.printf(
"Using Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
108, 100, 33 },
byteArrray);
}
@Test
public void whenGetBytesWithStandardCharset_thenOK() {
final String inputString = "Hello World!";
final Charset charset = StandardCharsets.UTF_16;
byte[] byteArrray = inputString.getBytes(charset);
System.out.printf(
"Using Standard Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 },
byteArrray);
}
@Test
public void whenEncodeWithCharset_thenOK() {
final String inputString = "Hello World!";
final Charset charset = StandardCharsets.UTF_8;
byte[] byteArrray = charset.encode(inputString).array();
System.out.printf(
"Using encode with Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
108, 100, 33, 0 },
byteArrray);
}
@Test
public void whenUsingCharsetEncoder_thenOK()
throws CharacterCodingException {
final String inputString = "Hello World!";
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith(new byte[] { 0 });
byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString)).array();
System.out.printf(
"Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n",
encoder, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
108, 100, 33, 0 },
byteArrray);
}
}