From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../hjp5/examples/ByteKit.java | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/ByteKit.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/ByteKit.java') diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/ByteKit.java b/Master/Reference Architectures and Patterns/hjp5/examples/ByteKit.java new file mode 100644 index 0000000..778a7d8 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/ByteKit.java @@ -0,0 +1,56 @@ +/** + * ByteKit + * + * Einfache Klasse zur Umwandlung zwischen int, char und + * vorzeichenlosen Bytes. + */ +public class ByteKit +{ + /** + * Wandelt value (0 <= value <= 255) in ein byte um. + */ + public static byte fromUnsignedInt(int value) + { + return (byte)value; + } + + /** + * Wandelt c in ein byte um. Das High-Byte wird ignoriert. + */ + public static byte fromChar(char c) + { + return (byte)(c & 0xFF); + } + + /** + * Betrachtet value als vorzeichenloses byte und wandelt + * es in eine Ganzzahl im Bereich 0..255 um. + */ + public static int toUnsignedInt(byte value) + { + return (value & 0x7F) + (value < 0 ? 128 : 0); + } + + /** + * Betrachtet value als vorzeichenloses byte und wandelt + * es in ein Unicode-Zeichen mit High-Byte 0 um. + */ + public static char toChar(byte value) + { + return (char)toUnsignedInt(value); + } + + /** + * Liefert die Binaerdarstellung von value. + */ + public static String toBitString(byte value) + { + char[] chars = new char[8]; + int mask = 1; + for (int i = 0; i < 8; ++i) { + chars[7 - i] = (value & mask) != 0 ? '1' : '0'; + mask <<= 1; + } + return new String(chars); + } +} \ No newline at end of file -- cgit v1.2.3