Click here to Skip to main content
16,003,265 members
Articles / Programming Languages / C#
Tip/Trick

Convert byte[] to short[]

Rate me:
Please Sign up or sign in to vote.
2.50/5 (4 votes)
2 Aug 2024CPOL 3.6K   41
Converting byte[] to short[]. Can be used when dealing with raw audio samples.
I came across an old question, some people on here get a little butt hurt when you post on an old question, so I decided to post a tip/trick on how, in case someone is searching for the same.
C#
static unsafe short[] ToInt16Array(byte[] data)
{
    //the length of data should be an even number
    int length = data.Length >> 1;
    short[] array = new short[length];
    fixed(byte* ptr = data) 
    {
        short* value = (short*)ptr;
        for (int i = 0; i < length; i++, value++) 
        {
            array[i] = (short)*value;
        }
    }
    return array;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
GeneralRe: Still not good enough Pin
charles henington4-Aug-24 18:50
charles henington4-Aug-24 18:50 
AnswerCode sample?! Pin
Sergey Alexandrovich Kryukov5-Aug-24 5:25
mvaSergey Alexandrovich Kryukov5-Aug-24 5:25 
GeneralRe: Still not good enough Pin
charles henington4-Aug-24 18:59
charles henington4-Aug-24 18:59 
GeneralRe: Still not good enough Pin
George Swan4-Aug-24 20:40
mveGeorge Swan4-Aug-24 20:40 
AnswerThis is not a quide Pin
Sergey Alexandrovich Kryukov5-Aug-24 5:38
mvaSergey Alexandrovich Kryukov5-Aug-24 5:38 
GeneralRe: This is not a quide Pin
George Swan5-Aug-24 10:02
mveGeorge Swan5-Aug-24 10:02 
AnswerArgue correctly Pin
Sergey Alexandrovich Kryukov5-Aug-24 10:28
mvaSergey Alexandrovich Kryukov5-Aug-24 10:28 
GeneralRe: Argue correctly Pin
George Swan5-Aug-24 14:07
mveGeorge Swan5-Aug-24 14:07 
Evidence based opinion is not a manipulative trick it's best practice. What really is manipulative is the use of down votes to discourage any poster that has the temerity to voice a contrary opinion. I do not believe that you are participating in that sort of vindictive behaviour, Sergey. But I have to ask because my responses to your posts immediately attract a down vote of -16 points from a single high status individual. So, at the risk of incurring yet another -16 points, I will post this and end the discussion. You may not get an up vote from me but you can rest assured that you will never get a down vote. You have kindly pointed out some of my faults and I have many; but indulging in denigrating punitive nonsense is not one of them.

AnswerRe: Argue correctly Pin
Sergey Alexandrovich Kryukov5-Aug-24 14:25
mvaSergey Alexandrovich Kryukov5-Aug-24 14:25 
AnswerRe: Convert Using A Binary Operator. Pin
charles henington5-Aug-24 7:47
charles henington5-Aug-24 7:47 
GeneralReposting old tricks and techniques Pin
Jalapeno Bob3-Aug-24 3:59
professionalJalapeno Bob3-Aug-24 3:59 
GeneralRe: Reposting old tricks and techniques Pin
charles henington3-Aug-24 4:56
charles henington3-Aug-24 4:56 
GeneralRe: Reposting old tricks and techniques Pin
Jalapeno Bob4-Aug-24 8:14
professionalJalapeno Bob4-Aug-24 8:14 
AnswerUseful? Pin
Sergey Alexandrovich Kryukov4-Aug-24 18:26
mvaSergey Alexandrovich Kryukov4-Aug-24 18:26 
GeneralRe: Useful? Pin
charles henington4-Aug-24 18:49
charles henington4-Aug-24 18:49 
AnswerPretty useful? Pin
Sergey Alexandrovich Kryukov5-Aug-24 5:44
mvaSergey Alexandrovich Kryukov5-Aug-24 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.