Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 13
Registered: ‎10-06-2011
My Device: BlackBerry Bold
My Carrier: Telefonica

Read double from byte array

Hi, a little help here, on my project I'm recording an audio signal that I need to decode in my software, however for that I need the double value of each point of the signal, this value is in fact a number smaller that 0, since I already build this on Android, and there I can use the ByteArray.readFloat() method,

 

However since javaME does not have a ByteArray class I'm using the byte[] that I get from the recorder _output.toByteArray() and to read the double values from I'm using

 

floatValue = ( ( pcm[i++] & 0xff ) << 24 ) | ( ( pcm[i++] & 0xff ) << 16 ) | ( ( pcm[i++] & 0xff ) << 8 ) | ( pcm[i++] & 0xff );

 however I get really large numbers in the floatValue, like 5.85279E9, and I know for a fact that the float representation for these bytes are numbers between 1 and -1,

 

any thoughts on why is this wrong??

 

many thanks in advance

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Read double from byte array

I suspect that you think this code is recreating the internal bit pattern used by a float and then the assignment will overlay it bit at a time.  In fact, I think the compiler will see the right hand side as an int, and then convert the int to a float.

 

Try

 

floatvalue = Float.intBitToFloat(.....);