Re: How to read binary data
In article
<7f5d25c9-6f4c-405c-b292-a1ebdfb36fe5@d38g2000prn.googlegroups.com>,
Dominik G <tnorgd@gmail.com> wrote:
OK, so now I do this:
for(int i = 0;i < 10;i++) {
ch = is.readByte();
System.out.print(ch + " ");
}
and the result is:
56 0 0 0 77 84 89 75 76 73 76 78 71 75 84 76 75 71 69 84
In particular my first four bytes contain the 56 I am looking for. But
what is the "official" way to get that value (with no hacking)? I may
hack on this first integer, i.e. to read bytes and convert them to int
by hand in such a way that gives mi a proper result, but I am afraid
this doesn't work in the case of doubles which are later in the file.
You can get seqlen using readIntLittleEndian():
<http://mindprod.com/jgloss/endian.html#INT>
Skip that many bytes of seqstr
Read the data into a byte[][]
Unscramble the bytes using a mapping[]
Convert each double's byte[] to a long
Convert the long using Double.longBitsToDouble()
Less flexibly, you can just exec the perl and parse stdout using
Double.valueOf() or Scanner:
<code>
import java.io.*;
/** @author John B. Matthews */
class ExecTest {
public static void main (String[] args) {
String s;
try {
Process p = Runtime.getRuntime().exec("./parse.pl");
// read from the process's stdout
BufferedReader stdout = new BufferedReader (
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null) {
// Parse using Double.valueOf() or Scanner
}
// read from the process's stderr
BufferedReader stderr = new BufferedReader (
new InputStreamReader(p.getErrorStream()));
while ((s = stderr.readLine()) != null) {
System.err.println(s);
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
System.err.println("Exit value: " + p.waitFor());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
</code>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>