• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

writing file but not correctly.

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
dervisakyuz
Asked:
dervisakyuz
  • 3
  • 2
1 Solution
 
CEHJCommented:
You don't need to do all that manual manipulation. Just use a DataInputStream and call readShort on it
0
 
dervisakyuzAuthor Commented:
could you give a small example
0
 
rama_krishna580Commented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
CEHJCommented:
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
 
dervisakyuzAuthor Commented:
thank you very much
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now