Search code examples
c#castingshortushort

convert short[] to ushort[]


I need to convert short[] to ushort[] in C#

public ushort[] Pixels16bits;

 GrayscalePixelDataS16 Pixeldata = PixelDataFactory.Create(img.PixelData, 0) as GrayscalePixelDataS16; //Pixeldata has short[] values

Pixels16bits = pixeldata; //Here's where I struggle, I need to convert to ushort prior to assign to pixels16bits

Thanks


Solution

  • I see a couple options:

    img.PixelData.Cast<ushort>().ToArray();
    

    Or perhaps:

    img.PixelData.Select(Convert.ToUInt16).ToArray();
    

    Both take advantage of LINQs ability to apply a transform function to a collection.