short数组转int数组
short []a={...};
int []b=new int[a.Length];
for(int i=0;i<a.Length;i++)
{
b[i]=(int)a[i];
}同理short数组转float数组
short []a={...};
float []b=new float[a.Length];
for(int i=0;i<a.Length;i++)
{
b[i]=(float)a[i];
}
short []a={...};
int []b=new int[a.Length];
for(int i=0;i<a.Length;i++)
{
b[i]=(int)a[i];
}同理short数组转float数组
short []a={...};
float []b=new float[a.Length];
for(int i=0;i<a.Length;i++)
{
b[i]=(float)a[i];
}
比如
for(int i=0;i<short1.Length;i++)
{
int1[i]=short1[i*2]*65536+(ushort)short1[i*2+1];
}
转float也一样,先把short[2]拆成4个byte,然后用BitConvert.ToSingle()转
我应该是解决了,虽然还不懂意思。
int x = 12;
short high_x = (short)((x & 0xffff0000) >> 16);
short low_x = (short)(x & 0xffff);
int y = high_x * 65536 + (ushort)low_x;float怎么转呢?继续求助
//byte[] b = BitConverter.GetBytes(f);
//float f1 = BitConverter.ToSingle(b, 0);话说,移位和与运算不明白的话,你就像3楼给的代码那样,用乘法算就好了,等你能明白了,再用那些高大上的写法
不要逻辑还没搞明白,就纠结底层CPU几个运算周期的效率问题,那是根本毫无意义的行为
这代码过一个月,你自己能看懂?
short1=int1/65536;
short2=int1%65536;
其实就是对65536做除法,商是高字,余数是低字
其实就是对256做除法嘛
用左移位右移位效率是很高,CPU内部其实乘法除法就是通过移位来实现的
但是你不理解的话,还是用你能理解的方法编程比较好
short[] s = { 1, 2 };
int[] i = new int[1];
float[] f = new float[1];
Buffer.BlockCopy(s, 0, i, 0, 4);
Buffer.BlockCopy(s, 0, f, 0, 4);
Console.WriteLine("{0} {1}", i[0], f[0]);