JAVA-1522 Split core-java-modules/core-java module
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.encoding;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
|
||||
public class CharacterEncodingExamples {
|
||||
|
||||
static String readFile(String filePath, String encoding) throws IOException {
|
||||
File file = new File(filePath);
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
try (InputStreamReader isr = new InputStreamReader(new FileInputStream(file), encoding)) {
|
||||
int data;
|
||||
while ((data = isr.read()) != -1) {
|
||||
buffer.append((char) data);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
static String convertToBinary(String input, String encoding) throws UnsupportedEncodingException {
|
||||
byte[] bytes = input.getBytes(encoding);
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (int b : bytes) {
|
||||
buffer.append(Integer.toBinaryString((b + 256) % 256));
|
||||
buffer.append(" ");
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
static String decodeText(String input, Charset charset, CodingErrorAction codingErrorAction) throws IOException {
|
||||
CharsetDecoder charsetDecoder = charset.newDecoder();
|
||||
charsetDecoder.onMalformedInput(codingErrorAction);
|
||||
return new BufferedReader(
|
||||
new InputStreamReader(
|
||||
new ByteArrayInputStream(input.getBytes()), charsetDecoder)).readLine();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.hexToAscii;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class HexToAsciiUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenHexToAscii() {
|
||||
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
|
||||
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
|
||||
|
||||
assertEquals(asciiString, hexToAscii(hexEquivalent));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAsciiToHex() {
|
||||
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
|
||||
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
|
||||
|
||||
assertEquals(hexEquivalent, asciiToHex(asciiString));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private static String asciiToHex(String asciiStr) {
|
||||
char[] chars = asciiStr.toCharArray();
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (char ch : chars) {
|
||||
hex.append(Integer.toHexString((int) ch));
|
||||
}
|
||||
|
||||
return hex.toString();
|
||||
}
|
||||
|
||||
private static String hexToAscii(String hexStr) {
|
||||
StringBuilder output = new StringBuilder("");
|
||||
for (int i = 0; i < hexStr.length(); i += 2) {
|
||||
String str = hexStr.substring(i, i + 2);
|
||||
output.append((char) Integer.parseInt(str, 16));
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Convert Hex to ASCII in Java](http://www.baeldung.com/java-convert-hex-to-ascii)
|
||||
Binary file not shown.
Reference in New Issue
Block a user