Converting Binary File to Text File

Hello,

I'm having trouble reading a binary file and converting it into readable text. The Binary file is a list of numbers, 0 thru 256, in 8-bit format. The code I have for it is:

<PRE>

import java.io.*;

import java.io.File;

import java.lang.*;

import java.util.*;

import java.sql.*;

public class ReadBinary {

// **********************************************************

public ReadBinary() {

}

// **********************************************************

public static void main(String args[]) {

try {

RandomAccessFile raf = new RandomAccessFile("input.bin","rw");

for (int x=0;x<1000;x++)

System.out.println(Byte.toString(raf.readByte()));

}

catch (Exception e) {

System.out.println("Error found");

System.exit(0);

}

}

// **********************************************************

}

</PRE>

I have a text version of the Binary file, but my program output does not match at all.

Thanks,

--Edriss

[1098 byte] By [edriss] at [2007-9-30 20:56:23]
# 1

> I have a text version of the Binary file, but my

> program output does not match at all.

For example? Your file has what byte in it and your program output what, which was different than you expected how?

My guess is that since you said each entry is an 8-bit number of range (0-256), and you're reading each into a (signed) Byte (of range -128 to +127) to start with.

warnerja at 2007-7-7 2:29:00 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for your reply.I was thinking the same problem about the signed and unsigned. for some that are 0, i get 127 instead. how do i fix that problem.Thanks,--Edriss
edriss at 2007-7-7 2:29:00 > top of Java-index,Java Essentials,Java Programming...
# 3
byte b = raf.readByte(); // -128 to 127 rangeint i = b & 0xff; // 0 to 255 range
warnerja at 2007-7-7 2:29:00 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks for your help.I was able to solve the problem I was having. I also found a website that does conversion of java objects. Here's the link if anyone's interested: http://mindprod.com/converter.htmlBest of luck,--Edriss
edriss at 2007-7-7 2:29:00 > top of Java-index,Java Essentials,Java Programming...
# 5
Holy ****.Edriss...* Does his own research.* Thanks those who responded to his question.* Posts the solution he found, that others may benefit.It seems he should be on the lecture circuit teaching certain cretins how to use these forums.
jverd at 2007-7-7 2:29:00 > top of Java-index,Java Essentials,Java Programming...