Recently in the graduation design, do is about speech recognition of mobile phone applications. In the process of processing audio, it is found that you need to process the audio with a short array, which may be out of bounds with byte. However, the Java read file is not read once to the API in the short array, either a read short, or a byte array that is manually converted to a short array. The former behavior and C + + behavior is the opposite, mainly high and low position problem, so can only take the latter.
See Code:
Public Short[] Bytearray2shortarray (byte[] Data,intitems) {
Short[] RetVal=New Short[items];
for (intI=0; I<retval.length; I++)
Retval[i]= ( Short) ((Data[i*2]&0xFF) |(Data[i*2+1]&0xFF) <<8);
returnRetVal;
}
Although code execution is right now, it is important to note that the data element should be preceded by a bit with 0xff, or the result of One-third is not correct. For what reason, please the great God to give advice!
After the data element is taken out to the first and 0xff to a bit, or out of the results of One-third is not correct.
# #不熟悉JAVA, guess that's the reason
Byte is implicitly converted to the signed int type when the bitwise operation is performed, so
The first is 0, the value is the same (0x7E = 0x0000007E)
The first is 1, the value changes (0xd3 = 0xffffffd3)
If the first place is 1 to do bit or operation, the result will be a problem
If the &0xff is executed first, then the 0xFFFFFFXX becomes 0x000000xx, guaranteeing the correct result.
How to convert a byte array to a short array (go)