• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to char

 
Naresh Rajan
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Naresh Rajan
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx it worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic