This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
In a program I have written, I read a photo file in InputStream. I convert the int I get in the Inputstream to Hexadecimal values using Integer.toHexString().
Later I am converting the hexadecimal values to int and then I am trying to regenerate into char. Some values do not get regenerated to the original char. I tried typecasting to char. i also tried to use Character.fromDigit(digit,radix).I am not able to understand what the radix means. It always returns a '.' I need to know what this radix means and how do I use them
It is totally unclear to me what you are trying to do. Why not give an example with a few numbers/bytes/characters/etc? I don't see how a "photo file" can contain "chars".
There is no emoticon for what I am feeling!
Joined: Sep 08, 2005
if u try to open a photo file using textpad or any editor you will be able to see junk characters. these characters when read using a file input stream it gives u the Ascii value of each character. each ascii value is then converted into its corresponding hex code. this hex code is then later reused to regenerate the photo.
The problem here is when i try to type cast some ascii values to char for eg. (char)145 returns ? (char)129 also returns ?
but it is supposed to return a different character. it happens to many other values as well.
Files that store photos (for example, JPEG or TIFF files) are binary files. They do not contain ASCII characters. If you open such a file with a text editor, the text editor doesn't know what to do with the binary data and shows bytes that it can't convert to characters as "?".
Why do you want to open those files with a text editor? It's still not very clear what you're trying to accomplish. Can you explain it step by step?
char in Java is 2-byte (Unicode). For binary files (such as most photos formats) you need 1-byte values, so the need to convert it to byte, not char.
(And don't use any Readers or Writers for binary files.)
Converting int to byte can be done using typecasting (in this case).
EDIT: To output the bytes correctly use OutputStream's write(int) method, PrintStream's print(char) may convert it incorrectly. You don't need to convert that int to anything since write method already accepts int. EDIT: I tried it with and it works. prints only ?.
[ April 21, 2006: Message edited by: Vlado Zajac ]
[ April 21, 2006: Message edited by: Vlado Zajac ] [ April 21, 2006: Message edited by: Vlado Zajac ]