Skip to content

Instantly share code, notes, and snippets.

@KVinS
Created May 25, 2016 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KVinS/bc4f4aaa1f2ea198801be19937e8ad6f to your computer and use it in GitHub Desktop.
Save KVinS/bc4f4aaa1f2ea198801be19937e8ad6f to your computer and use it in GitHub Desktop.
package ru.kvins;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Пример ввода:");
byte[] exampleArray = "Example \uD83D\uDE01 Пример".getBytes(StandardCharsets.UTF_8);
for (byte anExampleArray : exampleArray) System.out.print(anExampleArray + " ");
System.out.println();
System.out.println("Для выхода совершите пустой ввод.");
System.out.println("Пожалуйста, введите последовательность байт через пробел:");
while (sc.hasNextLine()) {
String inputString = sc.nextLine();
if (inputString.isEmpty()) {
return;
} else {
LinkedList<Byte> bytesList = new LinkedList<>();
String[] parsingBytes = inputString.trim().split(" ");
for (String byteString : parsingBytes) {
try {
byte b = Byte.parseByte(byteString);
bytesList.add(b);
} catch (NumberFormatException e) {
System.err.println("Не байт во вводе. Процесс обработки остановлен! '" + byteString + "'");
return;
}
}
if (bytesList.size() > 0) {
int i = 0;
byte[] enteredBytes = new byte[bytesList.size()];
for (Byte b : bytesList) {
enteredBytes[i++] = b;
}
printCharacters(enteredBytes);
}
}
}
}
private static void printCharacters(byte[] characters) {
try {
String parsedCharacters = new String(characters, StandardCharsets.UTF_8);
for (int i = 0; i < parsedCharacters.length(); i++) System.out.println(Integer.toHexString((int) parsedCharacters.charAt(i)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment