• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read, translate to hex, write?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the methods in java.io.PrintWriter
 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic