Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help with Text into ASCII

  • 16-02-2008 6:42pm
    #1
    Closed Accounts Posts: 1,080 ✭✭✭


    Ok this is a lab I have to do.
    • Produce a system that takes in a chunk of text and converts it into ASCII
    • Re-use code from last semester to create a program that takes in a file input.txt and outputs a binary ASCII version called output.txt
    • ASCII conversion tables are available online
    • Be able to encode or decode a file - you must have this working by the end of week 2

    Here is my two classes
    import java.io.*;
    
    public class Translator
    {
    
        static String[] array = new String[200]; //make sure your String array is big enough to hold all the data
        static int nElems = array.length;
    
        public void loadTable(String tablefile)
        {
            File newFile = new File(tablefile);
            getContents(newFile);
        }
    
        public void toASCII(String input, String output)
        {
    
        }
    
        public void toBinary(String input, String output)
        {
            File inputFile = new File(input);
            File outputFile = new File(output);
        }
    
    
        public static String getContents(File aFile)
      {
        //...checks on aFile are elided
        StringBuffer contents = new StringBuffer();
    
        //declared here only to make visible to finally clause
        BufferedReader input = null;
        try {
          //use buffering, reading one line at a time
          //FileReader always assumes default encoding is OK!
          input = new BufferedReader( new FileReader(aFile) );
          String line = null; //not declared within while loop
          /*
          * readLine is a bit quirky :
          * it returns the content of a line MINUS the newline.
          * it returns null only for the END of the stream.
          * it returns an empty String if two newlines appear in a row.
          */
          int i = 0;
          while (( line = input.readLine()) != null && i < 10000)
          {
            array[i]=line;
            i++;
            contents.append(System.getProperty("line.separator"));
          }
          nElems=i;
        }
        catch (FileNotFoundException ex)
        {
          ex.printStackTrace();
        }
        catch (IOException ex){
          ex.printStackTrace();
        }
        finally
        {
          try
          {
            if (input!= null)
            {
              //flush and close both "input" and its underlying FileReader
              input.close();
            }
          }
          catch (IOException ex)
          {
            ex.printStackTrace();
          }
        }
        return contents.toString();
      }
    
    
      public static void setContents(File aFile) throws FileNotFoundException, IOException
      {
    
        //declared here only to make visible to finally clause; generic reference
        Writer output = null;
        try {
          //use buffering
          //FileWriter always assumes default encoding is OK!
          output = new BufferedWriter( new FileWriter(aFile) );
          int i=0;
          while(array[i]!=null){
            output.write( array[i] );
            output.write(System.getProperty("line.separator"));
            i++;
          }
        }
        finally
        {
          //flush and close both "output" and its underlying FileWriter
          if (output != null) output.close();
        }
      }
    }
    
    public interface TranslatorInterface
    {
    	public void loadTable(String tablefile);
    	public void toASCII(String input, String output);
    	public void toBinary(String input, String output);
    }
    

    I dont understand how I am ment to put the INPUT into ascii. :confused:


    Any help?


Comments

  • Moderators, Music Moderators Posts: 23,363 Mod ✭✭✭✭feylya


    I would assume that they want to you change the letters to their ASCII number equivelents ie a=97, b=98 and so on (http://www.asciitable.com/). You will have to run through each character of your input file and convert it into it's number, then output to the Output file. Failing that, ask your lecturer...


  • Closed Accounts Posts: 1,080 ✭✭✭eamoss


    Well ok I suck at programming :o

    No really, 'Hello, World' is about the only thing I can program :o

    These are the Design Tips
    • There should be an entirely separate class called Translator with an encode method which takes in a character and returns a binary String (e.g. 10001010) or a decode method which does the opposite
    • The main part of the program should read in the file, process it one character at a time, call the Translator (either encode or decode methods) and then save the results to output.txt
    • Store the ASCII values in a file called table.txt and load them up using Translator.loadTable() – each code on a different line
      A 1010001
      B 0100011
    • Hint: use the FileReader from the sorting lab we did - look up FileReader on java.sun.com for a description of how it can be used

    One of the lads gave me this.
    00100000
    = 00111101
    ! 00100001
    " 00100010
    &#163; 10100011
    $ 00100100
    % 00100101
    ^ 01011110
    & 00100110
    * 00101010
    ( 00101000
    ) 00101001
    _ 01011111
    + 00101011
    - 00101101
    : 00111010
    ; 00111011
    { 01111011
    } 01111101
    [ 01011011
    ] 01011101
    @ 01000000
    ' 00100111
    ~ 01111110
    # 00100011
    < 00111100
    > 00111110
    ? 00111111
    , 00101100
    . 00101110
    / 00101111
    A 01000001
    B 01000010
    C 01000011
    D 01000100
    E 01000101
    F 01000110
    G 01000111
    H 01001000
    I 01001001
    J 01001010
    K 01001011
    L 01001100
    M 01001101
    N 01001110
    O 01001111
    P 01010000
    Q 01010001
    R 01010010
    S 01010011
    T 01010100
    U 01010101
    V 01010110
    W 01010111
    X 01011000
    Y 01011001
    Z 01011010
    a 01100001
    b 01100010
    c 01100011
    d 01100100
    e 01100101
    f 01100110
    g 01100111
    h 01101000
    i 01101001
    j 01101010
    k 01101011
    l 01101100
    m 01101101
    n 01101110
    o 01101111
    p 01110000
    q 01110001
    r 01110010
    s 01110011
    t 01110100
    u 01110101
    v 01110110
    w 01110111
    x 01111000
    y 01111001
    z 01111010
    

    But I dont how to read in one text file and then check for each car in another and then put the new file.

    Is there any examples of this?


  • Moderators, Music Moderators Posts: 23,363 Mod ✭✭✭✭feylya


    OK, basically what you need to do is create another class (another .java file) called Translator. In that, you will have (at least) 3 functions:
    encode()
    decode()
    loadTable()

    Encode willl be passed a single character from the main class which is will then compare to an array of characters (that loadTable() has populated) and return the binary value for it.
    Decode will do the opposite.
    LoadTable will read a file with the binary values and store them in a 2d array.

    It should be fairly simple to implement but I can't help you much beyond that, due to the homework clause in the Charter


Advertisement