Click here to Skip to main content
15,998,673 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.3K   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

 
AnswerLearn first Pin
Sergey Alexandrovich Kryukov4-Aug-24 8:07
mvaSergey Alexandrovich Kryukov4-Aug-24 8:07 
AnswerStill not good enough Pin
Sergey Alexandrovich Kryukov4-Aug-24 8:02
mvaSergey Alexandrovich Kryukov4-Aug-24 8:02 
GeneralRe: Still not good enough Pin
George Swan4-Aug-24 10:51
mveGeorge Swan4-Aug-24 10:51 
AnswerRe: Still not good enough Pin
Sergey Alexandrovich Kryukov4-Aug-24 12:43
mvaSergey Alexandrovich Kryukov4-Aug-24 12:43 
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 

Thanks for your response. I was hoping that you would give a specific implementation of the generic method so that benchmark tests could be run on the three methods. I can write my own version but your implementation may be more exemplary than mine. With regard to your reference to the documentation page and the question, 'Isn't it enough?'. Could I refer you to the solution guide lines point 4? "Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid." That's brilliant advice and I suggest that we all follow it.


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 
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.