有一个字节数组,byte[6],前面4个放了其他信息,然后再把一个int(<65536)放入后面的两个字节中
如:
string id=“125”
int i=Convert.ToInt32(id)
如何将i存入后面两个字节中?

解决方案 »

  1.   

    byte[4]=(byte)(i<<8);
    byte[5]=(byte)i;
    你试试
      

  2.   

    int a = 1000;
                byte[] _Value = BitConverter.GetBytes(a);_Value[0] _Value[1] 附值到你的byte[6] 就可以了
      

  3.   

    int是4个字节而不是两个字节。
    两个字节用short 或者ushort.
      

  4.   

    byte[] myByteArray = System.BitConverter.GetBytes(myInt);
      

  5.   

    首先int是4字节 不能存入2字节的空间
      

  6.   

    string转化成short后,如何转成2字节的bytes?
      

  7.   

    byte []byt=new byte[2];
    byt[0]=(byte)(i/256);
    byt[1]=(byte)i;
      

  8.   

    int i= 125;
    byte[] byInputData =  BitConverter.GetBytes(i);  IntPtr ptr = Marshal.AllocHGlobal(4); 
        byte[] b= new byte[4]{1,2,3,4};
        Marshal.WriteInt32(ptr, u);
        Marshal.Copy(ptr,b,0,4);
        Marshal.FreeHGlobal(ptr);b[0] = (byte)(i);
    b[1] = (byte)(i >> 8);
    b[2] = (byte)(i >> 16);
    b[3] = (byte)(i >> 24);