Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

reading a binary file and decoding it

Posted on 2007-10-18
16
Medium Priority
?
1,322 Views
Last Modified: 2013-11-23
i have a binary file that i am trying to read.
what i do is this:

int thismuch = 8;
               
for (int j = 0; j < 10; j++) {
           
for(int i = 0; i < thismuch; i++){
System.out.print(dis.readUnsignedByte() + " * ");
}
                      
System.out.println();
          
}

or something like:

FileInputStream fis = new FileInputStream(source);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);      
              
int length = 0;
byte[] result = null;
               
String sbuf = null;

length = 22;
result = new byte[length];
dis.readFully(result,0,length);
String fr1 = getStringFromBytes(result, 0, length);
           
System.out.println("RESULT: " + result + " FR1: " + fr1);

and the result is something like in the first case, etc...
225 * 92 * 194 * 1 * 0 * 195 * 1 * 14 *
255 * 39 * 10 * 198 * 8 * 6 * 7 * 146 *
9 * 0 * 135 * 152 * 247 * 237 * 16 * 211 *
3 * 5 * 8 * 35 * 244 * 5 * 220 * 3 *
1 * 53 * 36 * 209 * 2 * 0 * 0 * 196 *
1 * 1 * 197 * 1 * 0 * 202 * 2 * 193 *
208 * 203 * 2 * 16 * 4 * 222 * 9 * 77 *
83 * 67 * 49 * 32 * 32 * 32 * 32 * 32 *
213 * 5 * 51 * 25 * 129 * 96 * 20 * 223 *
48 * 3 * 0 * 0 * 0 * 247 * 6 * 255 *

but the actual output i should get is something along the lines:
(i will add the pipes, just have to read the records...)

A1|16|0|0|607029900000777|7750002|3509066933752104|

so how do i read/convert the resluts above to actually get the values: A1, 16, 0, 0, 607029900000777, etc...

the fileds are fixed length all of them
what am i doing wrong??

thank you for the help
0
Comment
Question by:qwekovaqwe
  • 9
  • 7
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20106941
You need to follow the binary format of your file and read fields in according lengths. You're just reading each byte
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20106946
Actually i don't know exactly what you're doing since you haven't posted getStringFromBytes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20106959
>>the fileds are fixed length all of them

If the fields are all the same length then it'll be a lot easier to follow this of course:

>>You need to follow the binary format of your file and read fields in according lengths.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:qwekovaqwe
ID: 20107080
CEHJ i get giberish when i do that
e.g.

int thismuch = 8;
               
for (int j = 0; j < 10; j++) {
           
for(int i = 0; i < thismuch; i++){
//System.out.print(dis.readUnsignedByte() + " * ");
length = 3;
result = new byte[length];
dis.readFully(result,0,length);
String fr1 = getStringFromBytes(result, 0, length);
System.out.println("RESULT: " + result + " FR1: " + fr1);
}

System.out.println();
}

and the result is:
RESULT: [B@182f0db FR1: \
RESULT: [B@192d342 FR1:
RESULT: [B@6b97fd FR1:
RESULT: [B@1c78e57 FR1:
RESULT: [B@5224ee FR1:
RESULT: [B@f6a746 FR1:
RESULT: [B@15ff48b FR1:
RESULT: [B@affc70 FR1:

RESULT: [B@1e63e3d FR1:
RESULT: [B@1004901 FR1:
RESULT: [B@1b90b39 FR1:
RESULT: [B@18fe7c3 FR1:
RESULT: [B@b8df17 FR1:
RESULT: [B@13e8d89 FR1:
RESULT: [B@1be2d65 FR1:
RESULT: [B@9664a1 FR1:

RESULT: [B@1a8c4e7 FR1:
RESULT: [B@1172e08 FR1:
RESULT: [B@cf2c80 FR1: MS
RESULT: [B@1729854 FR1: C
RESULT: [B@6eb38a FR1:
RESULT: [B@1cd2e5f FR1:
RESULT: [B@19f953d FR1:
RESULT: [B@1fee6fc FR1: `

RESULT: [B@1eed786 FR1:
RESULT: [B@187aeca FR1:
RESULT: [B@e48e1b FR1:
RESULT: [B@12dacd1 FR1:
RESULT: [B@1ad086a FR1:
RESULT: [B@10385c1 FR1:
RESULT: [B@42719c FR1: d
RESULT: [B@30c221 FR1: N_

RESULT: [B@119298d FR1:
RESULT: [B@f72617 FR1:
RESULT: [B@1e5e2c3 FR1:
RESULT: [B@18a992f FR1:
RESULT: [B@4f1d0d FR1:
RESULT: [B@1fc4bec FR1:
RESULT: [B@dc8569 FR1:
RESULT: [B@1bab50a FR1:

RESULT: [B@c3c749 FR1:
RESULT: [B@150bd4d FR1:
RESULT: [B@1bc4459 FR1:
RESULT: [B@1f33675 FR1:
RESULT: [B@7c6768 FR1:
RESULT: [B@1690726 FR1:
RESULT: [B@5483cd FR1:
RESULT: [B@9931f5 FR1:

RESULT: [B@19ee1ac FR1:
RESULT: [B@1f1fba0 FR1: M
RESULT: [B@1befab0 FR1: SC
RESULT: [B@13c5982 FR1:
RESULT: [B@1186fab FR1:
RESULT: [B@14b7453 FR1:
RESULT: [B@c21495 FR1: Y
RESULT: [B@1d5550d FR1: `

RESULT: [B@c2ea3f FR1:
RESULT: [B@a0dcd9 FR1:
RESULT: [B@1034bb5 FR1:
RESULT: [B@15f5897 FR1:
RESULT: [B@b162d5 FR1:
RESULT: [B@1cfb549 FR1:
RESULT: [B@186d4c1 FR1: d
RESULT: [B@f9f9d8 FR1: {N

RESULT: [B@1820dda FR1: _
RESULT: [B@15b7986 FR1:
RESULT: [B@87816d FR1:
RESULT: [B@422ede FR1:
RESULT: [B@112f614 FR1:
RESULT: [B@1d9dc39 FR1:
RESULT: [B@93dcd FR1:
RESULT: [B@b89838 FR1:

RESULT: [B@111a3ac FR1:
RESULT: [B@110b053 FR1:
RESULT: [B@a83b8a FR1:
RESULT: [B@dd20f6 FR1:
RESULT: [B@19efb05 FR1:
RESULT: [B@723d7c FR1:
RESULT: [B@22c95b FR1:
RESULT: [B@1d1acd3 FR1:

what it is writing out is the right most column of the hex editor i use to view the biary file in, the one with ascii representation...

and this is what i use for bytestostring
public static String getStringFromBytes(byte[] ba, int offset, int baLength) {
char[] buf = new char[baLength];
int j = 0;

for (int i = offset; i < baLength; i++) {
if (((int) ba[i]) == 0x00) {
break;                        //end of string
}
else if (((int) ba[i]) >= 0x40) {
buf[j++] = (char) ba[i];
}
}
return new String(buf, 0, j);
}

thnx
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20107097
You need to do not

>>System.out.println("RESULT: " + result + " FR1: " + fr1);

but

System.out.println("RESULT: " + Arrays.toString(result) + " FR1: " + fr1);
0
 
LVL 3

Author Comment

by:qwekovaqwe
ID: 20107108
i did that and i got the same results as with
readByte()

e.g.
RESULT: [-31, 92, -62] FR1: \
RESULT: [1, 0, -61] FR1:
RESULT: [1, 14, -1] FR1:
RESULT: [39, 10, -58] FR1:
RESULT: [8, 6, 7] FR1:
RESULT: [-110, 9, 0] FR1:
RESULT: [-121, -104, -9] FR1:
RESULT: [-19, 16, -45] FR1:

i just dont know how to get the values i need from it
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 20107128
As i said you need to read the file fields as they were written, in the correct length
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20107177
If you run this code it will produce the outcome you showed above:

private static void readFile() throws IOException {
      DataInputStream in = new DataInputStream(new URL("http://byrnegaunt.co.uk/misc/bin.dat").openStream());
      StringBuilder sb = new StringBuilder();
      long n = 0L;
      sb.append(String.format("%02X", in.readLong()));
      sb.append("|");
      sb.append(in.readLong());
      sb.append("|");
      sb.append(in.readLong());
      sb.append("|");
      sb.append(in.readLong());
      sb.append("|");
      sb.append(in.readLong());
      sb.append("|");
      sb.append(in.readLong());
      sb.append("|");
      sb.append(in.readLong());
      sb.append("|");
      in.close();
      System.out.println(sb);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20107178
>>      long n = 0L;

can be deleted
0
 
LVL 3

Author Comment

by:qwekovaqwe
ID: 20107182
ok. assuming the first field has a length of two characters (i have specifications only on how the output should look) namely a hex number of max FF.

so is this the way to read it and display it?


length = 2; // <<<<<<<<<<<<<<<<<<
result = new byte[length];
dis.readFully(result,0,length);
String fr1 = getStringFromBytes(result, 0, length);
                      
System.out.println("RESULT: " + Arrays.toString(result) + " FR1: " + fr1); // <<<<<<<<<<<<


thnx
0
 
LVL 3

Author Comment

by:qwekovaqwe
ID: 20107187
k disregard the previous post i'll try your code :P
thnx
0
 
LVL 3

Author Comment

by:qwekovaqwe
ID: 20107202
this is the result i get from the file

E15CC20100C3010E|-61068224291797102|648667437405638867|217589107508239363|87016247903912132|72555677113516737|-3401622825723885235|
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20107236
You haven't used my code then ;-)
0
 
LVL 3

Author Comment

by:qwekovaqwe
ID: 20107307
i did.

here is the complete thing:
File source = new File("D:/GAM0607.ama");
              FileInputStream fis = new FileInputStream(source);
              BufferedInputStream bis = new BufferedInputStream(fis);
              DataInputStream in = new DataInputStream(bis);
                  
                  StringBuilder sb = new StringBuilder();
                  sb.append(String.format("%02X", in.readLong()));
                  sb.append("|");
                  sb.append(in.readLong());
                  sb.append("|");
                  sb.append(in.readLong());
                  sb.append("|");
                  sb.append(in.readLong());
                  sb.append("|");
                  sb.append(in.readLong());
                  sb.append("|");
                  sb.append(in.readLong());
                  sb.append("|");
                  sb.append(in.readLong());
                  sb.append("|");
                  in.close();
                  System.out.println(sb);
0
 
LVL 3

Author Comment

by:qwekovaqwe
ID: 20107332
anyhow thnx for the help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20107352
>>i did.

Well you used your file with my code. As i mentioned, you need to read the file based on how the file was written. My code assumes fixed 8 byte fields.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question