?
Solved

writing file but not correctly.

Posted on 2006-05-14
6
Medium Priority
?
416 Views
Last Modified: 2012-05-05
Hi again;

b.raw and c.raw read into array then i convert them int then use them in function

then function returns integer then i convert int to byte array

then i write this byte array to file

but this file is not what i want.

What do u think about this?



public static void main(String args[]) throws Exception{
                
                AEC aec = new AEC();
              

                try {
                      InputStream is = new FileInputStream("b.raw");// connect file
                      InputStream is1 = new FileInputStream("c.raw");// connect 2nd file
                      FileOutputStream      fos = new FileOutputStream("desiredsignal.raw"); // desired
                      byte[] bDizi = new byte[64];
                      byte[] bDizi1 = new byte[64];
                      int s ;
                      while( (s = is.read(bDizi)) !=-1 && (s = is.read(bDizi1)) !=-1 ) {
                            int i =0;
                                  
                            for(i=0; i<s; i++  ) {
                             s0=byteArrayToInt( bDizi,0);
                            s1=byteArrayToInt( bDizi1,0);
                                  s0 = aec.doAEC(s0,s1);
                                  System.out.print(s0 );
                                  System.out.print(s1 );
                            byte[]      bDizi3 = intToByteArray(s0);
                                  fos.write(bDizi3);
                                  
                                  
                            }

                      }
                      is.close();
                  
                  is1.close();
                           
                  fos.close();// baglantiyi kapat

                } catch(IOException e) {
                      System.err.println(e);
                }

------------------------------------------------
this is  byteArrayToInt :
public static int byteArrayToInt(byte[] b, int offset) {
              int value = 0;
              for (int i = 0; i < 4; i++) {
                  int shift = (4 - 1 - i) * 8;
                  value += (b[i + offset] & 0x000000FF) << shift;
              }
              return value;
          }

this is ntToByteArray :
 public static  byte[] intToByteArray(int so) {
              byte[] buf = new byte[4];
              for (int z = 0; z < 4; z++) {
                  int offset = (buf.length - 1 - z) * 8;
                  buf[z] = (byte) ((so >>> offset) & 0xFF);
              }
              return buf;
      }
0
Comment
Question by:dervisakyuz
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16677763
You don't need to do all that manual manipulation. Just use a DataInputStream and call readShort on it
0
 

Author Comment

by:dervisakyuz
ID: 16677778
could you give a small example
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 16677802
Hi,

/**
  convert byte array to int
  */
  static public int baToInt(byte[] b, int strt, int end) throws ProcessingException
  {
    if (end > b.length - 1)
    {
      throw new ProcessingException( "baToInt: strt = " + strt + ", end = " + end + ", b.length = " + b.length);
    }
    int l = end - strt + 1;
    int i = 0;
    for (int j = 0; j < l; j++)
    {
      int p = strt + l - j - 1;
      // get int value of unsigned byte into k
      if (p > b.length - 1)
      {
        throw new ProcessingException("baToInt: p = " + p + ", strt = "+ strt+ ", end = " + end + ", l = "+ l + ", j = " + j + ", i = " + i);
      }
      int k = getUnsignedByte(b[p]);
      int n = pow(256, j);
      i += n * k;
    }
    return i;
  }

For more info...
http://www.koders.com/java/fid4B4796D35E70F3E4CB61ED235761544C024B00F4.aspx

R.K
0
Independent Software Vendors: 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 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16677811
Try something like


DataInputStream in = null;
File f = new File("b.raw");
long fileLength = f.length();
if (fileLength % 4 != 0) {
      throw new RuntimeException("File corrupted");
}
try {
      in = new DataInputStream(FileInputStream("b.raw"));// connect file
      for(int i = 0;i < fileLength / 2;i++) {
        short left = in.readShort();
        short right = in.readShort();
        
        // ETC
      }
}
catch(IOException e) {
}
finally {
      try { in.close(); } catch(Exception e) { e.printStackTrace(); }
}

0
 

Author Comment

by:dervisakyuz
ID: 16677847
thank you very much
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16677850
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

809 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