aspose file tools*
The moose likes Java in General and the fly likes int to char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "int to char" Watch "int to char" New topic
Author

int to char

Naresh Rajan
Ranch Hand

Joined: Sep 08, 2005
Posts: 45
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

Please help in this regard


Am not a pro and thats why am here!!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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
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.

I hope I have been clear enough.

FYI: this logic has been forced on me
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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
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: 245
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 ]
Naresh Rajan
Ranch Hand

Joined: Sep 08, 2005
Posts: 45
Thanx it worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int to char