10 Replies Latest reply: May 2, 2010 3:51 PM by 807580 RSS

    converting Byte [] into float []

    801494
      Hi, I read the contents of a file into a byte array and am now trying to convert it into a float array.

      here is the code I am using
      public static float [] bytetofloat(byte [] convert){
      
          float [] data = new float [convert.length/2];
          for(int i = 0;i<convert.length;i+=2){
              for(int j = 0; j <data.length;j++){
      
      
      
              short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
              float valueAsFloat = (float)valueAsShort;
              System.out.println(valueAsFloat);
              valueAsFloat = data[j];
              System.out.println(data[j]);
               }
          }
      can anyone see anythign wrong with the way I am doing this? I cant see anythign wrong but need to make sure its fine before I can continue.

      any advice on this or a better way to do it would be much appreciated.
        • 1. Re: converting Byte [] into float []
          JosAH
          Maybe you can use a ByteBuffer, wrap it around your byte[] convert data, create a FloatBuffer view and extract a float[] from that FloatBuffer.

          kind regards,

          Jos
          • 2. Re: converting Byte [] into float []
            801494
            thanks, ill look into it.

            do you see any problems with the code I have put forward?
            It should work fine but I am unsure what the output is supposed to be
            • 3. Re: converting Byte [] into float []
              796440
              ultiron wrote:
              can anyone see anythign wrong with the way I am doing this?
              Does it work? I mean, you did run a bunch of test data through it, and verify that it produces the correct values, right?

              That nested loop looks fishy. You should only need one loop.

              Edited by: jverd on May 2, 2010 10:51 AM
              • 4. Re: converting Byte [] into float []
                801494
                yea your right
                my new code is
                public static void bytetofloat(byte [] convert){
                
                    float [] data = new float [convert.length/2];
                    for(int i = 0;i<convert.length;i+=2){
                     
                
                
                
                        short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
                        float valueAsFloat = (float)valueAsShort;
                        System.out.println(valueAsFloat);
                
                         }
                }

                for anyone interested in this. I havent confirmed the results are accurate however.
                • 5. Re: converting Byte [] into float []
                  JosAH
                  Do your float values only take up two bytes?

                  kind regards,

                  Jos
                  • 6. Re: converting Byte [] into float []
                    801494
                    I'm pretty sure they do. The way im doing it is by taking 2 byte values and changing them to a 16 bit float.
                    the way the bytes are shift they should only take up 16 bits.
                    • 7. Re: converting Byte [] into float []
                      JosAH
                      ultiron wrote:
                      I'm pretty sure they do. The way im doing it is by taking 2 byte values and changing them to a 16 bit float.
                      the way the bytes are shift they should only take up 16 bits.
                      Ordinary floats take up four bytes (at least in C, C++ and Java they do), but your floats may take up two bytes, only you know ...

                      kind regards,

                      Jos
                      • 8. Re: converting Byte [] into float []
                        796440
                        ultiron wrote:
                        I'm pretty sure they do. The way im doing it is by taking 2 byte values and changing them to a 16 bit float.
                        the way the bytes are shift they should only take up 16 bits.
                        It's not that simple.

                        First, a float in Java is always 4 bytes. The fact that it has an integer value that can fit into two bytes is irrelevant

                        Second, floating point representation is not the same 2s complement that's used for byte, short, int, and long, so you're not just shifting the bits.

                        For eample:
                        1,  int: 00000000 00000000 00000000 00000001
                        1, float: 00111111 10000000 00000000 00000000
                        
                        -1,  int: 11111111 11111111 11111111 11111111 
                        -1, float: 10111111 10000000 00000000 00000000
                        Having said that, you're casting, so your step of going from short to float is correct. It doesn't just shift; it does conversions like the above. The caveats are:

                        1) Is your conversion from bytes to short correct? That depends on what those bytes are supposed to be. If they're big-endian representations of 2-byte, 2s-complement numbers, then your code looks correct. You'll still have to test it of course. You'll want to focus on corner cases.
                        00 00 --> 0
                        00 01 --> 1
                        10 00 --> 4096
                        7f ff --> 32767
                        80 00 --> -32768
                        ff ff --> -1

                        2) Can float hold all of short's values? I think it can. It obviously can't hold all of int's values, but I think it's precision is sufficient to cover short.

                        By the way, is there a reason you're using float instead of double? Unless you're in an extremely memory-constrained environment, there's no reason to use double.
                        • 9. Re: converting Byte [] into float []
                          801494
                          Cheers for the explanation.

                          The reason that I am trying to convert a byte array into a float array is because I have a Function which takes in a float array as input and I am trying to use it here

                          It may help if I tell you its a Fast Fourier Transform function, the values in the byte array are big-endian and were taken from a sound file input stream.

                          I did noticed the different representations and was trying to work out a way around it.


                          00 00 --> 0
                          00 01 --> 1
                          10 00 --> 4096
                          7f ff --> 32767
                          80 00 --> -32768
                          ff ff --> -1



                          are these examples byte ->float examples or byte -> short?

                          thanks a lot

                          jord
                          • 10. Re: converting Byte [] into float []
                            807580
                            Jeff and Jos - take a look at the OP's other two threads on this topic.