Click here to Skip to main content
16,017,857 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 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

Si bien estamos más que dispuestos a ayudar a quienes están estancados, eso no significa que estemos aquí para hacerlo todo por usted. No podemos hacer todo el trabajo, o le pagan por ello o es parte de sus calificaciones y no sería justo que lo hiciéramos todo por usted.

Por lo tanto, necesitamos que haga el trabajo y lo ayudaremos cuando se quede estancado. ¡Eso no significa que le daremos una solución paso a paso que pueda entregar!
Comience por explicar dónde se encuentra en este momento y cuál es el próximo paso en el proceso. Luego, cuéntenos qué ha intentado para que ese próximo paso funcione y qué sucedió cuando lo logró.

Si tienes problemas para empezar, esto puede ayudarte: Cómo escribir código para resolver un problema, una guía para principiantes[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

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