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

byte array to int

hi

i want to convert  byte[] to int;

do u think it is correct?




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;
}
0
dervisakyuz
Asked:
dervisakyuz
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
sciuriwareCommented:
int i = Integer.parseInt(new String(b));   // That's all

;JOOP!
0
 
CEHJCommented:
Looks OK
0
 
dervisakyuzAuthor Commented:
int i = Integer.parseInt(new String(b));   // That's all

i try but the error is


at java.lang.NumberFormatException.forInputString(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at AEC.main(AEC.java:351)

i think i will import java.sth.

i am beginner in java.
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!

 
CEHJCommented:
>>int i = Integer.parseInt(new String(b));   // That's all

You could only do that if the byte array happened to contain an ASCII representation of numbers. You need what you had already
0
 
rama_krishna580Commented:
Hi,

byte[] bytes = new byte[] {1,2,3,4};
int total = 0;
for(int i = 0;i < bytes.length; i++) {
int shifted = bytes[i] << i*8;
total = total | shifted;
}
System.out.println(total);

OR

/**
  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
 
hoomanvCommented:
dervisakyuz
I think your implementation is good enough
thats correct

why do you think its not correct ?
0
 
dervisakyuzAuthor Commented:
Because in this code i write a file
but it is not what i want.So my be the convertions not correct


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

                try {
                      InputStream is = new FileInputStream("b.raw");// dosyaya baglan
                      InputStream is1 = new FileInputStream("c.raw");// aynen
                      FileOutputStream      fos = new FileOutputStream("desiredsignal.raw"); // olusacak dosya
                      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);
                }
          
                
           float ambient = aec.getambient();
           float ambientdB = q2dB(ambient / 32767);
           System.err.println("Ambient = " + ambientdB  + " dB");
           
              return; }
           
0
 
hoomanvCommented:
the conversion is 100% correct

you can test it with this piece of code

public static void main(String[] args) throws Exception {
      int value = 0;
      byte[] b = {0, 0, 1, 0};
      for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (b[i] & 0x000000FF) << shift;
      }
      System.out.println(value);
}

byte[] b = {0, 0, 1, 0}; this is exactly the Big-endian representation of number 256 = 0x00000100

one thing you should consider is that some systems may use little-endian representation of bytes
That is, least significant byte should come first

learn about endians here
http://en.wikipedia.org/wiki/Endianness
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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