This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
I don't understand the whole problem, but the radix is the base of the number system. For example, for decimal, radix = 10, for hexadecimal, radix = 16.
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!
Naresh Rajan
Ranch Hand
Joined: Sep 08, 2005
Posts: 45
posted
0
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?
Ajay A Patil
Greenhorn
Joined: Apr 13, 2006
Posts: 22
posted
0
Naresh,
> (char)145 returns ? > (char)129 also returns ?
Try running the following code.
The output will be:
Actually, the ascii character 145 and 129 are displayed as '?' in the console.
This is only a display issue and perhaps you shouldnt worry about it. Reading your mail, I think you should have no issue in converting it into the hexadecimal string and generating your image.
Cheers, Ajay
Vlado Zajac
Ranch Hand
Joined: Aug 03, 2004
Posts: 244
posted
0
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 ]