/*This class provides 2 static methods to process 7-Bit ASCII chars int[] bits( char ch ) returns the binary digits of an ASCII char char character( int[] bits ) returns the ASCII char from bits Norman Pestaina CDA 4101 Assignment Spring 2017 */ import java.util.*; public class ASCII_Chars { //Returns the binary digits of a printable ASCII character // @Param ch: a printable ASCII char ' ' .. '~' public static int[] bits(char ch) { return binaryDigits( Character.hashCode(ch) ); } //Returns the printable ASCII char corresponding to given binary digits // @Param bits: An 8-element array of the bits of a printable ASCII char public static char character(int[] bits) { int code = 0; for (int digit : bits) code = code * 2 + digit; return Character.toChars(code)[0]; } //Helper: // Obtain the binary digits in a given unsigned byte integer // @param x: the given integer // @exception if x > 255 private static int[] binaryDigits(int x) { int[] digits = new int[8]; for (int i = 7; i >= 0; i--) { digits[i] = x % 2; x = x / 2; } if ( x != 0 ) throw new RuntimeException("Overflow 8 digits"); return digits; } //Throwaway //Run this to get a listing of all the ASCII printing characters public static void main(String[] args) { for (int code = 32; code < 127; code++) { char ch = Character.toChars(code)[0]; System.out.println(ch + " : " + Arrays.toString(bits(ch)) + " : " + character(bits(ch)) + " : " + String.format("%x", code) + " : " + code ) ; } } }