First: Read BinaryIn.java and BinaryOut.java about their methods before reading on!
Otherwise you won't know what is going on!
Note: This will vary between your implementations, and the following are just suggestions!
Just like Java's Scanner, you would first need to construct a new instance of the BinaryIn (I suppose you know that already)
To keep on reading a file until the end, BinaryIn provides a nice method called isEmpty()
which tells you if it has reached the end of file (EOF) or not. So, to start, you will probably have something like this:
BinaryIn in = new BinaryIn("ur.jpg");
while(!in.isEmpty()){
//do something with the input stream
}
And so for each char, you will probably throw it into some data structure and count their frequency.
The next thing you ask me is: Well, how do I print the binary value for the char?
Well, before doing that, you must first know (you probably already do) that a character is an integer.
Characters are encoded by 8-bit (1 byte) integers in the world of CompSci, using the ASCII coding standard.
2^7 = 128, so ASCII codes 128 characters, with the other bit reserved for special characters like ÿ.
Actually, you can do this in Java (and C):
int c = 'x';
printf("%d, %c", c, c);
And it will give you 120, x
, where 120 is the interger value for x, according to ASCII Coding.
A CHARACTER IS AN INTEGER !!!
So, don't be surprised when I ask you to read characters from an image. You are reading the bytes from the image, and storing them as characters.
Side Note: A standard 24-bit colored image stores the pixels in their Red, Green, Blue (RGB) values, each color gets 8 bits, and thus each color ranges from 0-255 (2^8 - 1). Mix those up, you get the wonderful colors
And so, you can easily do this:
while(!in.isEmpty()){
char c = in.readChar();
int i = (int)c;
System.out.printf("Decimal: %4d; Char: %4c\n", i, c);
}
Which, when reading the image, will give you:
Decimal: 255; Char: ÿ
Decimal: 216; Char: Ø
Decimal: 255; Char: ÿ
Decimal: 224; Char: à
Decimal: 0; Char:
Decimal: 16; Char:
Since not all ASCII characters are printable, it is normal to see blanks or ⍰. (Also varies between different OS)
Now you ask me: But the professor wanted the binary coding of the character for the frequency file
Well, you see, he wants the byte, in binary, not the character (counting characters from an image doesn't make much sense).
Java does provide you with a nice method from Integer
that parses the integer into its binary form, as a String, Integer.toBinaryString(int i)
.
And to pad 0's to the front, you can use String.format("%08d", Integer.parseInt(bin))
, where 0
tells java to pad 0's, 8
tells java the length of the string, and d
tells java the input is an integer
And so you have the following:
while(!in.isEmpty()){
char c = in.readChar();
int i = (int)c;
String bin = Integer.toBinaryString(i);
System.out.printf("Decimal: %4d; Char: %4c\n", i, c);
String binFormatt = String.format("%08d", Integer.parseInt(bin));
System.out.println("Binary: " + binFormatt);
}
Which, when reading the image, will give you:
Decimal: 255; Char: ÿ
Binary: 11111111
Parsed back char: ÿ
Decimal: 216; Char: Ø
Binary: 11011000
Parsed back char: Ø
Decimal: 255; Char: ÿ
Binary: 11111111
Parsed back char: ÿ
Decimal: 224; Char: à
Binary: 11100000
Parsed back char: à
Decimal: 0; Char:
Binary: 00000000
Parsed back char:
Decimal: 16; Char:
Binary: 00010000
Ok, now you know how to get the binary string for your frequency
file, onto BinaryOut
Well, you have your binary representation (the shorter code), and it's time to write it.
For your encoded file, you will be writing individual bits, to prevent binary out from padding 0's which ruins your effort.
One method that could be used is write(boolean x)
which you can use to write 1 (true) and 0 (false) as bits from your Huffman Code.
To write your decoded file, if you are looking to write binary strings as characters, here is how you can do it:
First: Cast the binary string to integer in base 2 Integer.parseInt(bin, 2)
.
Then: Cast the integer to char char outC = (char)Integer.parseInt(bin, 2);
.
Finally: Write Out out.write(outC);
.
When you finish writing, don't forget to close()
BinaryOut which flushes the stream and completes your output file.
Best,
Sifan Ye
There is an easier approach to write out boolean from a given String of binary representation of any binary number.
Integer.toBinaryString()
as you said will return a String of binary representation.try
System.out.print('A')
will print outA
try
System.out.print(Integer.toBinaryString((char) 'A'))
will print out1000001
Once you have this string, there is another way to write individual bits using
write(boolean x)
. You can traverse the string char by char, and write outtrue
orfalse
(1 or 0) based on each char.Say you stored the binary code of in a String variable
code
String code = "1000001";
Then you can travel through the String and write out each bit\
This achieves the same goal from another approach
Good Nite ComSci PEOPLE
Yihang Xu