-2

I am writing a program for Huffman Coding. I need to feed in 10 files, each with 500-1000 characters containing all 256 ASCII characters atleast 1 time. How am I gonna do it? Are there sample files on the internet which contain all 256 ASCII characters? Please Help!

Update: I could type the 255 ascii characters. But I can't seem to type NULL for some reason. It just doesn't show in the file. All other characters show for e.g. a delete is represented by a pentagon. I tried Alt+0.

Komal Rathi
  • 4,164
  • 13
  • 60
  • 98
  • 3
    you should be able to do this in a simple loop, you don't need a file from the internet. – tay10r Apr 17 '13 at 01:56
  • @TaylorFlores: I cant print some of the characters like NULL and delete..how do I do it – Komal Rathi Apr 17 '13 at 01:57
  • you will need to open and write to the file in binary mode. you CAN write null and delete. give me a moment to find the write link to send you – tay10r Apr 17 '13 at 01:59

2 Answers2

1

You can generate these files yourself - a relatively simple algorithm should be sufficient:

  • Go through numbers c from 0 to 255 sequentially
  • For each number c, generate a random number n in the range from 1 to 4, inclusive
  • Add n values of c to a list
  • If the number of items in the list is less than 500 (this is very unlikely) you can set c back to 0, and continue with the same algorithm until you cross the 500 count.
  • Apply the random shuffle algorithm to the list
  • Write the results to a file.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

I'm not a java programmer ( I don't know a bit of java ), I'm a c programmer. But, what you want to do is pretty simple. I found an example that you should check out.

http://www.java2s.com/Code/Java/File-Input-Output/Writesomedatainbinary.htm

You need to modify the code so that you are writing bytes, instead of integers. And you should do it in a loop. If it needs to be random, do as dasblinkenlight suggested. The following snippet I wrote replaces the one in the link I gave you.

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBinary {
  public static void main(String[] argv) throws IOException {

    String FILENAME = "binary.dat";
    DataOutputStream os = new DataOutputStream(new FileOutputStream(
        FILENAME));

    for( int i = 0; i < 256; i++ ) os.writeByte( (byte) i );

    os.close();
  }
}

You should also note that not all ascii values are printable characters. So, when you view the files, you wont be able to count 256 characters. But, you should be able to see the whole alphabet and 0 - 9.

j23
  • 160
  • 9
tay10r
  • 4,234
  • 2
  • 24
  • 44
  • I can't seem to type NULL for some reason. It just doesn't show in the file. All other characters show for e.g. a delete is represented by a pentagon. – Komal Rathi Apr 17 '13 at 03:05
  • there probably isn't a printable character for null. check your file size, if it's 256 then NULL is definitely in there. – tay10r Apr 17 '13 at 04:05
  • 1
    Might want to correct the ':' at the end of that for loop. – Bob Small May 10 '17 at 15:52
  • @BobSmall thanks, it's been corrected. – tay10r May 10 '17 at 21:21
  • 2
    byte in java is -127 to 128 so will it ever be 256 to stop the for-loop? I tried and it never stops :), change to some other type. Adjusting code – j23 Nov 23 '18 at 12:11