Click here to Skip to main content
16,020,628 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.9K   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

 
SuggestionMake it faster Pin
Hawkynt12-Aug-24 3:23
professionalHawkynt12-Aug-24 3:23 
GeneralRe: Make it faster Pin
charles henington13-Aug-24 15:46
charles henington13-Aug-24 15:46 
SuggestionRe: Make it faster Pin
Hawkynt14-Aug-24 23:00
professionalHawkynt14-Aug-24 23:00 
QuestionJose Pin
Jose Hernandez 20248-Aug-24 18:18
Jose Hernandez 20248-Aug-24 18:18 
AnswerRe: Jose Pin
OriginalGriff8-Aug-24 18:19
mveOriginalGriff8-Aug-24 18:19 
SuggestionHave you tried Buffer.BlockCopy? Pin
Daniele Rota Nodari4-Aug-24 23:51
Daniele Rota Nodari4-Aug-24 23:51 
AnswerConvertAll! Pin
Sergey Alexandrovich Kryukov5-Aug-24 6:14
mvaSergey Alexandrovich Kryukov5-Aug-24 6:14 
GeneralRe: ConvertAll! Pin
charles henington5-Aug-24 7:02
charles henington5-Aug-24 7:02 
AnswerRe: ConvertAll! — timing Pin
Sergey Alexandrovich Kryukov5-Aug-24 10:34
mvaSergey Alexandrovich Kryukov5-Aug-24 10:34 
GeneralRe: ConvertAll! Pin
Daniele Rota Nodari5-Aug-24 8:03
Daniele Rota Nodari5-Aug-24 8:03 
AnswerRe: ConvertAll! — two reasons Pin
Sergey Alexandrovich Kryukov5-Aug-24 10:36
mvaSergey Alexandrovich Kryukov5-Aug-24 10:36 
GeneralRe: Have you tried Buffer.BlockCopy? Pin
George Swan6-Aug-24 3:39
mveGeorge Swan6-Aug-24 3:39 
GeneralRe: Have you tried Buffer.BlockCopy? Pin
Daniele Rota Nodari6-Aug-24 8:18
Daniele Rota Nodari6-Aug-24 8:18 
SuggestionPretty bad Pin
Sergey Alexandrovich Kryukov4-Aug-24 7:59
mvaSergey Alexandrovich Kryukov4-Aug-24 7:59 
GeneralRe: Pretty bad Pin
charles henington4-Aug-24 18:44
charles henington4-Aug-24 18:44 
GeneralRe: Pretty bad Pin
George Swan4-Aug-24 22:59
mveGeorge Swan4-Aug-24 22:59 
GeneralRe: Pretty bad Pin
charles henington5-Aug-24 7:06
charles henington5-Aug-24 7:06 
AnswerLet's sort it out Pin
Sergey Alexandrovich Kryukov5-Aug-24 6:09
mvaSergey Alexandrovich Kryukov5-Aug-24 6:09 
QuestionConvert Using A Binary Operator. Pin
George Swan3-Aug-24 7:44
mveGeorge Swan3-Aug-24 7:44 

Thanks for the interesting tip. Here is a version that uses a bit of bit shifting.

C#
static  short[] ToInt16ArrayB(byte[] data)
{
    //Calculate the required array size
    int arrayLength = (data.Length / 2)+data.Length%2;
    short[] array = new short[arrayLength];
   for(int i=0;i<data.Length;i++)
    {
        //When i%2 is 1 it's the high order byte of the short
        //So shift it to the left by 8 bits
        int shiftN = (i % 2) * 8;
        short shifted= (short)(data[i]<<shiftN);
        //OR in the required byte.
        //use integer division arithmetic to index the correct array member
        array[i / 2] = (short)(array[i / 2] | shifted);
    }
    return array;
}

Edit. A better solution has been posted here but I shall retain this method in the hope that it may be instructive.



modified 6-Aug-24 9:54am.

AnswerRe: Convert Using A Binary Operator. Pin
charles henington3-Aug-24 18:26
charles henington3-Aug-24 18:26 
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 

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.