Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast binary resource to short array?

Tags:

c#

I need to process a binary resource (contents of a binary file). The file contains shorts. I can't figure how to cast the result of accessing that resource to a short array though:

short[] indexTable = Properties.Resources.IndexTable;

doesn't work; I can only use

byte[] indexTable = Properties.Resources.IndexTable;

Using Array.Copy wouldn't work since it would convert each byte from the array returned by accessing the resource to a short.

How do I solve this problem please (other than manually converting the byte array)? Is there a way to tell C# that the resource is of type short[] rather than byte[]?

I had even tried to edit the project's resx file and change the resources' data types to System.UInt16, but then I got the error message that now constructor could be found for them.

Using VS 2010 Express and .NET 4.0.

like image 714
Razzupaltuff Avatar asked Jun 04 '26 04:06

Razzupaltuff


1 Answers

You should use BinaryReader:

using (var reader = new BinaryReader(new MemoryStream(Properties.Resources.IndexTable)))
{
    var firstValue = reader.ReadInt16();
    ...
}
like image 164
Kirk Woll Avatar answered Jun 05 '26 16:06

Kirk Woll