aspose file tools*
The moose likes Java in General and the fly likes Writing Hex Values to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing Hex Values to a file" Watch "Writing Hex Values to a file" New topic
Author

Writing Hex Values to a file

Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
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

Regards

Nikhil


ban$al
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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

Joined: Jan 24, 2005
Posts: 60
Hi,

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

Nikhil

import java.io.*;

public class ReadBinaryData {

public static void main(String args[]){

try{
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");
System.exit(0);
}


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
is.close();
s = new String( bytes, "cp420" );


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

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

}



}// End of main

}//End of class
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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 ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Isn't this the same question as in your other post? It's rather confusing to have them both running at the same time.
 
Don't get me started about those stupid light bulbs.
 
subject: Writing Hex Values to a file
 
Similar Threads
Java I/O Performance
Hex values format
Hex values format problem
Reading binary data
Unable to read Arabic data