• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read, translate to hex, write?  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read a character from an input file, translate it to hex and write the hex characters to an output file. Any suggestions?

What I have so far
import java.io.*;
class TrivialApplication
{ public static void main(String args[])
{
int i;
String inf = "p3in.txt";
String ouf = "pout.txt";
FileInputStream fin;
FileOutputStream fou;

try
{ fin = new FileInputStream(inf);//set up input file
}
catch(FileNotFoundException fnf)
{ System.out.println(inf);
return;
}

try
{ fou = new FileOutputStream(ouf);//set up output file
}
catch(FileNotFoundException fnf)
{ System.out.println(fnf);
return;
}

try
{
do
{
i = fin.read(); //read a char in; write a char out
if(i != -1)
{
???;

fou.write(?);
}
}
while(i != -1);
}
catch(IOException ioe)
{ System.out.println("File error");
}
fin.close();
fou.close();
}
}
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the character as an int. Look at the Integer class and see if there isn't a method just made for you.
 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Stan; but I tried
String s = Integer.toHexString(i);
already, but when I tried
fou.write(s);
that didn't work, because write() won't take a string as an arg;
is there a method that returns a hex as anything other than a String?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the methods in java.io.PrintWriter
 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike!
I changed all of my FileOutputStreams to FileWriters (and FileInputStreams to FileReaders) and that gave me access to a write() method that writes Strings!
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!