How to convert a byte array to a short array (go)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.