string to byte array

This commit is contained in:
DOHA 2018-10-16 23:04:15 +03:00
parent aebb463186
commit 8ef39f81b4
1 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package com.baeldung.java8.base64;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import javax.xml.bind.DatatypeConverter;
import org.junit.Test;
public class StringToByteArrayUnitTest {
@Test
public void whenConvertStringToByteArrayUsingStringClass_thenOk() {
final String originalInput = "test input";
byte[] result = originalInput.getBytes();
System.out.println(Arrays.toString(result));
assertEquals(originalInput.length(), result.length);
}
@Test
public void givenCharset_whenConvertStringToByteArrayUsingStringClass_thenOk() throws UnsupportedEncodingException {
final String originalInput = "test input";
byte[] result = originalInput.getBytes(StandardCharsets.UTF_16);
System.out.println(Arrays.toString(result));
assertTrue(originalInput.length() < result.length);
}
@Test
public void whenConvertStringToByteArrayUsingBase64Decoder_thenOk() {
final String originalInput = "dGVzdCBpbnB1dA==";
byte[] result = Base64.getDecoder().decode(originalInput);
assertEquals("test input", new String(result));
}
@Test
public void whenConvertStringToByteArrayUsingDatatypeConverter_thenOk() {
final String originalInput = "dGVzdCBpbnB1dA==";
byte[] result = DatatypeConverter.parseBase64Binary(originalInput);
assertEquals("test input", new String(result));
}
@Test
public void whenConvertStringToByteArray_thenOk(){
String originalInput = "7465737420696E707574";
byte[] result = DatatypeConverter.parseHexBinary(originalInput);
System.out.println(Arrays.toString(result));
assertEquals("test input", new String(result));
}
}