各位大虾,给一个十进制数字,例如1257,我们知道他的十六进制表示就是‘\x05f7’。小弟想问一下,如何直接知道这个1257的十六进制的高位和低位,因为我想把他的高位和低位分别存入数组的第0位和第1位:例如  intTemp = 1257;byte buf[];
buf[0] = '\x05'; -〉intTemp 的高位
buf[1] = '\xf7'; -〉intTemp 的高位

解决方案 »

  1.   

    use "BitConverter.GetBytes" method to convert int to bytes
      

  2.   

    by the way, an integer is composed of 4 bytes.
      

  3.   

    uint temp = 1257;byte[] buf = new byte[2];
    uint temp2  =  temp >> 8;//右移8位
    buf[0] = (byte)temp2;//'\x04'; -〉temp 的高位
    buf[1] = (byte)(temp - temp2);//'\xe5'; -〉temp 的低位
      

  4.   

    用移位操作:<<< 和 >>>
      

  5.   

    sorry,有错误
    uint temp = 1257;byte[] buf = new byte[2];
    uint temp2  =  temp >> 8;//右移8位
    buf[0] = (byte)temp2;//'\x04'; -〉temp 的高位
    buf[1] = (byte)(temp - (temp2 << 8));//'\xe9'; -〉temp 的低位
      

  6.   

    uint temp = 1257;byte[] buf = new byte[2];
    buf[0] = (byte)(temp & 255)
    buf[1] = (byte)((temp >> 8) & 255)
      

  7.   

    uint temp = 1257;byte[] buf = new byte[2];
    buf[0] = (byte)(temp & 255)
    buf[1] = (byte)((temp >> 8) & 255)正解