Ri 8 break case 9 ri 9 break case a ri 10 break case

This preview shows page 8 - 13 out of 13 pages.

r[i] = 8; break; case '9': r[i] = 9; break; case 'a': r[i] = 10; break; case 'b': r[i] = 11; break; case 'c': r[i] = 12; break; case 'd': r[i] = 13; break; case 'e':
r[i] = 14; break; case 'f': r[i] = 15; break; default: System.out.println("Problem with stringToData."); // Testing for erroneous requests. break; } } return r; } // 7. public static String toRleString(byte[] rleData) // Translates RLE data into a human-readable representation. // For each run, in order, it should display the run length in decimal (1-2 digits); // the run value in hexadecimal (1 digit); and a delimiter, , between runs. // (See examples in standalone section.) // Ex:toRleString(new byte[] { 15, 15, 6, 4 }) yields string "15 f:64". public static String toRleString(byte[] rleData) { String r = ""; for (int i = 0; i < rleData.length; i += 2) { r += rleData[i]; switch (rleData[i + 1]) { case 0://0 in decimal is 0 in hex r = r + '0'; break; case 1: r = r + '1'; break; case 2: r = r + '2'; break; case 3: r = r + '3'; break; case 4: r = r + '4'; break;
case 5: r = r + '5'; break; case 6: r = r + '6'; break; case 7: r = r + '7'; break; case 8: r = r + '8'; break; case 9: r = r + '9'; break; case 10: //10 in decimal is a in hex r = r + 'a'; break; case 11: r = r + 'b'; break; case 12: r = r + 'c'; break; case 13: r = r + 'd'; break; case 14: r = r + 'e'; break; case 15: r = r + 'f'; break; default: System.out.println("Problem with toRleString"); // For testing erroneous requests in console. break; } r += ':'; } return r.substring(0, r.length() - 1); }
// 8. public static byte[]stringToRle(String rleString) // Translates a string in human-readable RLE format (with delimiters) into RLE byte data. // (Inverse of #7)Ex:stringToRle("15 f:64") yields byte array{15, 15, 6, 4 } public static byte[] stringToRle(String rleString) { int colonLocation = 0; String temp = ""; for (int i = 0; i < rleString.length(); i++) { if (rleString.length() <= i + 2 && rleString.charAt(i + 2) == ':') { colonLocation = i + 2; } else { if (rleString.length() <= 3) { colonLocation = i + 3; } else { colonLocation = -1; } } if (colonLocation == 2) { temp += rleString.charAt(i); temp += rleString.charAt(i + 1); i += 3; } if (colonLocation == 3) { switch (rleString.charAt(i + 1)) { case '0': temp += 'a'; break; case '1': temp += 'b'; break; case '2': temp += 'c'; break; case '3': temp += 'd'; break; case '4':
temp += 'e'; break; case '5': temp += 'f'; break; default: System.out.println("Problem with stringToRle"); // Testing for erroneous requests in scanner. break; } temp += rleString.charAt(i + 2); i += 4; } if (colonLocation == -1) { if (i + 2 == rleString.length()) { temp += rleString.charAt(i); temp += rleString.charAt(i + 1); } else { switch (rleString.charAt(i + 1)) { case '0': temp += 'a'; break; case '1': temp += 'b'; break; case '2': temp += 'c'; break; case '3': temp += 'd'; break; case '4': temp += 'e'; break; case '5': temp += 'f'; break; default: System.out.println("Problem with else statement in stringToRle"); // Testing for erroneous data inscanner for else in stringToRle. break; }
temp += rleString.charAt(i + 2); } } } //temp has hex string return stringToData(temp); } } // Close Main Class.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture