将字节数组转换为短数组? [英] Convert a byte array to a short array?

查看:101
本文介绍了将字节数组转换为短数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,该代码将多个字节读取到数组中,然后将它们复制到短裤数组中.

I have the following code that reads a number of bytes into an array then copies them into an array of shorts.

//nInt16是以字节形式读取的16位整数的数量
//br是BinaryReader

    // nInt16 is the number of 16 bit integers in being read in the form of bytes
    // br is a  BinaryReader

byte [] data = br.ReadBytes(nInt16 * 2);
short [] arrInt16 = new short [nInt16];
Buffer.BlockCopy(data,0,arrInt16,0,nInt16 * 2) ;

    byte[] data = br.ReadBytes(nInt16 * 2);
    short[] arrInt16 = new short[nInt16];
    Buffer.BlockCopy(data, 0, arrInt16, 0, nInt16 * 2);

在C#中,是否有一种方法可以将数据读取到字节数组中,然后指向"字节.短数组而不是复制数据?类似于可以在C ++中完成的工作. (我意识到C#和C ++中的数组是完全不同的数据结构)

Is there a way, in C#, to read the data into a byte array and then "point" an array of short to it instead of copying the data? Similar to what can be done in C++. (I realize that arrays in C# and C++ are totally different data structrures)

谢谢.

大卫

推荐答案

我认为您无法做到,这有点违反类型安全"的规定,

I don't think you can do that, it kinds of go against "type safe"

也许您可以使用br.ReadInt16,但是您必须一次阅读它.

May be you can use br.ReadInt16, but you will have to read it one by one.


这篇关于将字节数组转换为短数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆