Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. /**********************************************
  2. * Name: Sophia Ciocca
  3. *
  4. * Compilation: javac ASCII.java
  5. * Execution: java ASCII
  6. *
  7. * Tests two functions, encode() and decode().
  8. * Turns a char into binary and then back into a char, according to ASCII library.
  9. *
  10. * % java ASCII A
  11. * 1000001
  12. * A
  13. ***********************************************/
  14.  
  15. public class ASCII {
  16.  
  17. public static void main(String[] args) {
  18. // convert command-line argument to bits
  19. boolean[] bits = encode(args[0]);
  20. printBooleanArray(bits);
  21.  
  22. // convert the bits back to a string
  23. String s = decode(bits);
  24. System.out.println(s);
  25. }
  26.  
  27.  
  28. //----------------------NON-MAIN FUNCTIONS----------------------//
  29.  
  30. // Given Function for Testing: print an array of booleans as 1s and 0s
  31. private static void printBooleanArray(boolean[] bits) {
  32. for (int i = 0; i < bits.length; i++) {
  33. // print a 1 for true or 0 for false
  34. if (bits[i]) System.out.print("1");
  35. else System.out.print("0");
  36. }
  37.  
  38. System.out.println(); // add a newline
  39. }
  40.  
  41.  
  42. /*
  43. * Name: encode
  44. * What it does: Turns a string into an array of boolean values corresponding
  45. * to the chars' ASCII values in binary.
  46. * PreCondition: the user has entered a String (thingToBeEncoded)
  47. * PostCondition: no side-effects; an array of booleans is created
  48. */
  49. public static boolean[] encode(String msg) {
  50.  
  51. //if String is null, return null
  52. if (msg == null) {
  53. return null;
  54. }
  55.  
  56. //create array of booleans; create slots, 7 * # of characters in string
  57. boolean[] binaryCharacters = new boolean[7 * msg.length()];
  58.  
  59. //go through each character and convert it into a binary number
  60. for (int i = 0; i < msg.length(); i++) {
  61.  
  62. //turn each character into a number
  63. int c = (int) msg.charAt(i);
  64.  
  65. //turning each number code for the char into bits
  66. //(converting decimal to binary)
  67. for (int j = 6; j >=0; j--) {
  68. binaryCharacters[7 * i + j] = (c % 2 == 1);
  69. c /= 2;
  70. }
  71. }
  72.  
  73. //return the message in binary ASCII numbers
  74. return binaryCharacters;
  75. }
  76.  
  77.  
  78. /*
  79. * Name: decode
  80. * What it does: Turns an array of boolean values into a string of
  81. * characters that correspond to the binary booleans
  82. * PreCondition: there is an array of booleans
  83. * PostCondition: no side-effects; a char is created (and some values of
  84. * array may be cut off if it is not divisible by 7)
  85. */
  86. public static String decode(boolean[] bits) {
  87.  
  88. //if boolean array is null, return null
  89. if (bits == null) {
  90. return "null";
  91. }
  92.  
  93. //create String & sum variables
  94. String backInCharacters = "";
  95.  
  96. //create variable to be the # of original characters
  97. int numberOfCharacters = bits.length / 7;
  98.  
  99. //go through the characters
  100. for (int i = 0; i < numberOfCharacters; i++) {
  101.  
  102. int characterSum = 0;
  103.  
  104. //go through each character's set of 7 numbers & convert to char
  105. for (int j = 6; j >= 0; j--) {
  106.  
  107. //if it's a 1, add it to the sum
  108. if (bits[7 * i + (6 - j)]) {
  109.  
  110. characterSum += Math.pow(2, j);
  111. }
  112. }
  113.  
  114. //turn the sum back into
  115. char charCharacterSum = (char) characterSum;
  116. backInCharacters += charCharacterSum;
  117. }
  118.  
  119. //return the original message in characters
  120. return backInCharacters;
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement