RleProgram.java - import java.util.Scanner import java.lang...

This preview shows page 1 - 3 out of 4 pages.

import java.util.Scanner; import java.lang.*; public class RleProgram { public static int hexToDecimal(char input){ int output = 0; if (input == 'A' || input == 'a'){ output = 10; } else if (input == 'B' || input == 'b'){ output = 11; } else if (input == 'C' || input == 'c'){ output = 12; } else if (input == 'D' || input == 'd'){ output = 13; } else if (input == 'E' || input == 'e'){ output = 14; } else if (input == 'F' || input == 'f'){ output = 15; } else{ output = Integer.parseInt(String.valueOf(input)); } return output; } public static String toHexString(byte[] data){ String hexString = ""; for (int dataIndex = 0; dataIndex < data.length; dataIndex++) { hexString += String.format("%X", data[dataIndex]); } return hexString.toLowerCase(); } public static int countRuns(byte[] flatData){ int runsCounted = 1; int countPerRun = 1; for (int flatDataIndex = 0; flatDataIndex < flatData.length - 1; flatDataIndex++){ if (flatData[flatDataIndex] == flatData[flatDataIndex+1]) { countPerRun++; } else { runsCounted++; countPerRun = 1; } if (countPerRun > 15){ runsCounted++; countPerRun = 1; } } return runsCounted; } public static byte[] encodeRle(byte[] flatData){ int runsCounted = countRuns(flatData); byte[] rleData = new byte[runsCounted * 2]; byte countPerRun = 1;
byte rleDataIndex = 0; for (int flatDataIndex = 0; flatDataIndex < flatData.length - 1; flatDataIndex++){ if (flatData[flatDataIndex] == flatData[flatDataIndex+1] && countPerRun < 15){ countPerRun++; } else { rleData[rleDataIndex] = countPerRun;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture