Click here to Skip to main content
16,017,351 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.8K   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 
charles henington wrote:
I'm self taught and prefer to write code vs using prebuilt solutions as it helps me to better understand…
This is a quite respectful attitude. I also prefer doing everything by myself. But let's sort things out, different things. One thing is the research done for understanding. Another thing is the publication with practical recommendations. It can be misleading. You did not tell the readers that it was done for your understanding of what's going on. You did not recommend a library method as a practically most beneficial. The failure to explain all that and to give the best recommendation is what gives a negative value to your publications. Please, no offense, better take it as friendly advice.

charles henington wrote:
unsafe code as it tends to be faster…
Sorry, “tends” is by far not enough. If you recommend some technique based on performance, you could do the research and provide your benchmarks. But you hardly can find any miracles, as in nearly all simple cases a standard library method call is faster. If you try out and find otherwise, it would really be interesting. By the way, the standard library methods can internally use unsafe when it is necessary, but using them can help you to avoid unnecessary unsafe code in the assemblies using them.

Thank you for understanding.
—SA
Sergey A Kryukov

QuestionConvert Using A Binary Operator. Pin
George Swan3-Aug-24 7:44
mveGeorge Swan3-Aug-24 7:44 
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.