怎样将short[]数组转换成byte[]数组

解决方案 »

  1.   

    short[] arrs = {1,2,3,4,5};
    byte[] arrb=new byte[arrs.Length];
    for(int i=0;i<arrs.Length;i++)
    {
    arrb[i]=(byte)arrs[i];
    }
      

  2.   

    byte[] t = Array.ConvertAll<short, byte>(arr, Convert.ToByte);arr就是那个Short数组
      

  3.   

    viena() :
    你的方法好像不行吧,short是16位的数据,arrb[i]=(byte)arrs[i];这样转换会有数据丢失。例如short[16]转换成byte数组,那么byte数组的长度应该是32才对啊
      

  4.   

    Ninputer(装配脑袋):我用的是vs 2003,有能在2003用的方法吗?
      

  5.   

    short[] arrs = { 1, 32767, -8, 4, 5 };
                byte[] arrb = new byte[arrs.Length * 2];            for (int i = 0; i < arrs.Length; i++)
                {
                    byte[] bi = System.BitConverter.GetBytes(arrs[i]);
                    arrb[2 * i] = bi[0];
                    arrb[2 * i + 1] = bi[1];
                }
      

  6.   

    short[] arrs = {1, 32767, -8, 4, 5};
    int len=arrs.Length*2;
    byte[] arrb=new byte[len];
    for(int i=0;i<len;i+=2)
    {
    short tmp=arrs[i/2];
    arrb[i]=(byte)(tmp>>8);
    arrb[i+1]=(byte)(tmp&0x00FF);
    }
      

  7.   

    short [] sht;
    ……
    byte [] byt;
    byt=System.Text.Encoding.UTF8.GetByte(sht.ToCharArray());
      

  8.   

    short[] arrshort = { 1, 2, 3, 4, 5 };
    byte[] arrbyte = new byte[arrs.Length * 2];
    for (int i = 0; i < arrs.Length; i++)
    {
          byte[] bi = System.BitConverter.GetBytes(arrs[i]);
          arrb[2 * i] = bi[0];
          arrb[2 * i + 1] = bi[1];
    }
      

  9.   

    short[] bbb=new short[10];
    ......
    byte[] ddd=(byte[])sht;
      

  10.   

    不知道楼主要干什么。
    这种事情在C++里面很简单,memcopy就成。
    但C#里面的数组和C++里面的数组完全不是一个概念,有些东西应该要转变思维。
      

  11.   

    short[] s = new short[10];
                byte[] b = new byte[20];
                unsafe
                {
                    fixed (short* ps = s)
                    {
                        byte* pb = (byte*)ps;
                        for (int i = 0; i < 20; i++,pb++)
                        {
                            b[i] = *pb;
                        }
                    }
                }或者
    unsafe
                {
                    fixed (byte* pb = b)
                    {
                        short* ps = (short*)pb;
                        for (int i = 0; i < 10; i++, ps++)
                        {
                            *ps = s[i];
                        }
                    }
                }
      

  12.   

    或者
    unsafe
                {
                    fixed (byte* pb = b)
                    {
                        System.Runtime.InteropServices.Marshal.Copy(s, 0, (IntPtr) pb, 10);
                        
                                        }
                }
      

  13.   

    或者
    unsafe
                {
                    fixed (short* ps = s)
                    {
                        System.Runtime.InteropServices.Marshal.Copy((IntPtr)ps, b, 0, 20);
                                        }
                }
      

  14.   

    dreadknightll(Dread Knight):
    ToCharArray()是string类的方法
      

  15.   

    short[] arrs = {1,2,3,4,5};
    byte[] arrb=new byte[arrs.Length*2];
    for(int i=0;i<arrs.Length;i++)
    {
        arrb[i*2]=(byte)(arrs[i]&0xFF00 >> 8);
        arrb[i*2+1] = (byte)(arrs[i]&0x00FF);
    }
      

  16.   

    难道C# 就不能MemCopy了?来个狠的short[] arrs = { 1, 2, 3, 4, 5 };
    byte[] arrb = new byte[arrs.Length * 2];
    Buffer.BlockCopy(arrs, 0, arrb, 0, arrb.Length);
      

  17.   

    .NET 1.1 2.0 通吃,就是不知道字节次序对不对,自己看吧