Skip to content

Commit be17633

Browse files
committed
BAEL-2313 Convert String to Byte Array and Reverse in Java
1 parent d578e45 commit be17633

2 files changed

Lines changed: 210 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.string.conversion;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import java.io.UnsupportedEncodingException;
7+
import java.nio.ByteBuffer;
8+
import java.nio.charset.CharacterCodingException;
9+
import java.nio.charset.Charset;
10+
import java.nio.charset.CharsetDecoder;
11+
import java.nio.charset.CodingErrorAction;
12+
import java.nio.charset.StandardCharsets;
13+
14+
import org.junit.Test;
15+
16+
public class ByteArrayToStringUnitTest {
17+
18+
@Test
19+
public void whenStringConstructorWithDefaultCharset_thenOK() {
20+
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
21+
108, 100, 33 };
22+
String string = new String(byteArrray);
23+
System.out.println(string);
24+
25+
assertNotNull(string);
26+
}
27+
28+
@Test
29+
public void whenStringConstructorWithNamedCharset_thenOK()
30+
throws UnsupportedEncodingException {
31+
final String charsetName = "IBM01140";
32+
final byte[] byteArrray = { -56, -123, -109, -109, -106, 64, -26, -106,
33+
-103, -109, -124, 90 };
34+
35+
String string = new String(byteArrray, charsetName);
36+
37+
assertEquals("Hello World!", string);
38+
}
39+
40+
@Test
41+
public void whenStringConstructorWithCharSet_thenOK() {
42+
final Charset charset = Charset.forName("UTF-8");
43+
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
44+
108, 100, 33 };
45+
46+
String string = new String(byteArrray, charset);
47+
48+
assertEquals("Hello World!", string);
49+
}
50+
51+
@Test
52+
public void whenStringConstructorWithStandardCharSet_thenOK() {
53+
final Charset charset = StandardCharsets.UTF_16;
54+
55+
final byte[] byteArrray = { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
56+
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 };
57+
58+
String string = new String(byteArrray, charset);
59+
60+
assertEquals("Hello World!", string);
61+
}
62+
63+
@Test
64+
public void whenDecodeWithCharset_thenOK() {
65+
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108,
66+
100, 33 };
67+
final Charset charset = StandardCharsets.UTF_8;
68+
69+
String string = charset.decode(ByteBuffer.wrap(byteArrray)).toString();
70+
System.out.println(string);
71+
72+
assertEquals("Hello World!", string);
73+
}
74+
75+
@Test
76+
public void whenUsingCharsetDecoder_thenOK()
77+
throws CharacterCodingException {
78+
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108,
79+
100, 33 };
80+
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
81+
82+
decoder.onMalformedInput(CodingErrorAction.IGNORE)
83+
.onUnmappableCharacter(CodingErrorAction.REPLACE)
84+
.replaceWith("?");
85+
86+
String string = decoder.decode(ByteBuffer.wrap(byteArrray)).toString();
87+
88+
assertEquals("Hello World!", string);
89+
}
90+
91+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.baeldung.string.conversion;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import java.io.UnsupportedEncodingException;
7+
import java.nio.CharBuffer;
8+
import java.nio.charset.CharacterCodingException;
9+
import java.nio.charset.Charset;
10+
import java.nio.charset.CharsetEncoder;
11+
import java.nio.charset.CodingErrorAction;
12+
import java.nio.charset.StandardCharsets;
13+
import java.util.Arrays;
14+
15+
import org.junit.Test;
16+
17+
public class StringToByteArrayUnitTest {
18+
19+
@Test
20+
public void whenGetBytesWithDefaultCharset_thenOK() {
21+
final String inputString = "Hello World!";
22+
final String defaultCharSet = Charset.defaultCharset().displayName();
23+
24+
byte[] byteArrray = inputString.getBytes();
25+
26+
System.out.printf(
27+
"Using default charset:%s, Input String:%s, Output byte array:%s\n",
28+
defaultCharSet, inputString, Arrays.toString(byteArrray));
29+
30+
assertNotNull(byteArrray);
31+
assert (byteArrray.length >= inputString.length());
32+
}
33+
34+
@Test
35+
public void whenGetBytesWithNamedCharset_thenOK()
36+
throws UnsupportedEncodingException {
37+
final String inputString = "Hello World!";
38+
final String charsetName = "IBM01140";
39+
40+
byte[] byteArrray = inputString.getBytes("IBM01140");
41+
42+
System.out.printf(
43+
"Using named charset:%s, Input String:%s, Output byte array:%s\n",
44+
charsetName, inputString, Arrays.toString(byteArrray));
45+
46+
assertArrayEquals(new byte[] { -56, -123, -109, -109, -106, 64, -26,
47+
-106, -103, -109, -124, 90 },
48+
byteArrray);
49+
}
50+
51+
@Test
52+
public void whenGetBytesWithCharset_thenOK() {
53+
final String inputString = "Hello World!";
54+
final Charset charset = Charset.forName("UTF-8");
55+
56+
byte[] byteArrray = inputString.getBytes(charset);
57+
58+
System.out.printf(
59+
"Using Charset:%s, Input String:%s, Output byte array:%s\n",
60+
charset, inputString, Arrays.toString(byteArrray));
61+
62+
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
63+
108, 100, 33 },
64+
byteArrray);
65+
}
66+
67+
@Test
68+
public void whenGetBytesWithStandardCharset_thenOK() {
69+
final String inputString = "Hello World!";
70+
final Charset charset = StandardCharsets.UTF_16;
71+
72+
byte[] byteArrray = inputString.getBytes(charset);
73+
74+
System.out.printf(
75+
"Using Standard Charset:%s, Input String:%s, Output byte array:%s\n",
76+
charset, inputString, Arrays.toString(byteArrray));
77+
78+
assertArrayEquals(new byte[] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
79+
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 },
80+
byteArrray);
81+
}
82+
83+
@Test
84+
public void whenEncodeWithCharset_thenOK() {
85+
final String inputString = "Hello World!";
86+
final Charset charset = StandardCharsets.UTF_8;
87+
88+
byte[] byteArrray = charset.encode(inputString).array();
89+
90+
System.out.printf(
91+
"Using encode with Charset:%s, Input String:%s, Output byte array:%s\n",
92+
charset, inputString, Arrays.toString(byteArrray));
93+
94+
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
95+
108, 100, 33, 0 },
96+
byteArrray);
97+
}
98+
99+
@Test
100+
public void whenUsingCharsetEncoder_thenOK()
101+
throws CharacterCodingException {
102+
final String inputString = "Hello World!";
103+
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
104+
encoder.onMalformedInput(CodingErrorAction.IGNORE)
105+
.onUnmappableCharacter(CodingErrorAction.REPLACE)
106+
.replaceWith(new byte[] { 0 });
107+
108+
byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString)).array();
109+
110+
System.out.printf(
111+
"Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n",
112+
encoder, inputString, Arrays.toString(byteArrray));
113+
114+
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
115+
108, 100, 33, 0 },
116+
byteArrray);
117+
}
118+
119+
}

0 commit comments

Comments
 (0)