• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Writing Hex Values to a file

Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am reading a file which contains EBCIDC data (English and Arabic) in binary format. My basic task is to convert this data in UTF-8 format.

I am able to read the data byte by byte. For each byte which is read I am able to find the Hex value for EBCIDC. So I am able to locate the corresponding Hex value in ASCII for (English characters and Numerals) and also for Arabic (using code page PC 1256).

Now my problem is how should I write the Hex Values (ASCII hex and Arabic Hex) to the file so that I should get the corresponding text on my o/p file.

Plz let me know


Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what you are trying to do is not necesary to program it from scratch. Java already offers several mechanisms to convert a file from one encoding to another.

The best option is to read the file using the EBCDIC encoding and write it to another using the UTF-8.

To do this create a java.io.Reader that uses EBCDIC this way:

In order to write the file in UTF-8 encoding, create a Writer that uses this enconding, this way

Now, the Strings that you read from the first file stream, write them into the second:

I am not absolutely sure that Cp500 corresponds to EBCDIC characters. Check the supported encodings in the JDK API Documentation.

You can see a list here.

Another option is to use java.nio.charset package. It contains clases to encode and decode characters from one character set to another.

Good luck, comrade!
Nikhil Bansal
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Foll is my code. Here I am reading bytes of data,specifying that it is in cp420 (EBCIDC Arabic) format and then writing to the o/p file in UTF-8 format.

However, there seems to be some problem.There are some junk characters getting written to the file esp the one's where the hex value is alphanumeric for ex 8D,8C etc. If the hex value is numeric then the o/p is correct.

What am I doing wrong in the code.

Also I need to insert a carriage return after every bytes of data read.

Plz help me guys


import java.io.*;

public class ReadBinaryData {

public static void main(String args[]){

File file = new File("D:\\MYDATA.DATASETS");
InputStream is = new FileInputStream(file);

File outfile = new File( "D:\\testHexFile.txt" );
FileOutputStream fout = new FileOutputStream( outfile);

String s = null;
long length = file.length();

if (length > Integer.MAX_VALUE) {
System.out.println("File is too large");

byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;

// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());

// Close the input stream and return bytes
s = new String( bytes, "cp420" );

byte[] output = s.getBytes( "UTF-8" );

// return bytes;
}catch(Exception e){
System.out.println("Exception e"+e.toString());


}// End of main

}//End of class
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By chance, did you read my previous post, Nikhil?

You practically disregarded everything I wrote.

If you are treating characters, why do you read the file using byte streams?. You want to convert a file from a format to another, use Reader and Writer classes instead. It's simpler. Unless you want to write you own decoder/encoder.

Simply open the file using the Cp420 enconding and write it to a file using the UTF-8 enconding.

As simple as this:

Read the JDK documentation about InputputStreamReader and OutputStreamWriter.

Good luck, buddy!
[ June 12, 2006: Message edited by: Edwin Dalorzo ]
Posts: 24594
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't this the same question as in your other post? It's rather confusing to have them both running at the same time.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!