Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.PatternSyntaxException;
- public class Frequency
- {
- public static String binary(String encodedFile){
- String temp[] = FileIO.load(encodedFile);
- String sentence = "";
- for(int i =0; i < temp.length; i++)
- {
- sentence = sentence + temp[i];
- }
- sentence = sentence.replaceAll(" ", "");//remove spaces
- String binaryString=""; //this stores the string of binary code
- for(int i=0; i < sentence.length(); i++){ //go through the sentence
- int decimalValue = (int)sentence.charAt(i); //convert to decimal
- String binaryValue = Integer.toBinaryString(decimalValue); //convert to binary
- for(int j=7;j>binaryValue.length();j--){
- binaryString+="0"; //this loop adds in those pesky leading zeroes
- }
- binaryString += binaryValue+" "; //add to the string of binary
- }
- // System.out.println(binaryString); //print out the binary
- int[] array = new int[256]; //an array to store all the frequencies
- for(int i=0; i < sentence.length(); i++){ //go through the sentence
- array[(int)sentence.charAt(i)]++; //increment the appropriate frequencies
- }
- return binaryString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement