我们这里就举byte[]类型和long类型的转换,其它数据类型用BitConverter类以此类推:
/// <summary> /// 字节数组转换为long类型 /// </summary> /// <param name="bytes">字节数组</param> /// <returns>long类型</returns> static long BytesToLong(byte[] bytes) { return BitConverter.ToInt64(bytes); } /// <summary> /// long类型转换为字节数组 /// </summary> /// <param name="number">long类型</param> /// <returns>字节数组</returns> static byte[] LongToBytes(long number) { return BitConverter.GetBytes(number); }