Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.PatternSyntaxException;
  3.  
  4. public class Frequency
  5. {
  6. public static String binary(String encodedFile){
  7.        
  8.     String temp[] = FileIO.load(encodedFile);
  9.        String sentence = "";
  10.        
  11.        for(int i =0; i < temp.length; i++)
  12.        {
  13.            sentence = sentence + temp[i];
  14.        }
  15.        
  16.        sentence = sentence.replaceAll(" ", "");//remove spaces
  17.        String binaryString="";      //this stores the string of binary code
  18.        
  19.        
  20.        for(int i=0; i < sentence.length(); i++){        //go through the sentence
  21.            int decimalValue = (int)sentence.charAt(i);      //convert to decimal
  22.            String binaryValue = Integer.toBinaryString(decimalValue);      //convert to binary
  23.            for(int j=7;j>binaryValue.length();j--){
  24.                binaryString+="0";           //this loop adds in those pesky leading zeroes
  25.             }
  26.            binaryString += binaryValue+" "; //add to the string of binary
  27.        }
  28.       // System.out.println(binaryString);    //print out the binary
  29.        
  30.              
  31.        int[] array = new int[256];      //an array to store all the frequencies
  32.        
  33.        for(int i=0; i < sentence.length(); i++){    //go through the sentence
  34.            array[(int)sentence.charAt(i)]++;    //increment the appropriate frequencies
  35.            
  36.        }
  37.        
  38.        return binaryString;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement