问题描述
我想以100%的可靠性在C#中将长字节转换为8个字节。
public static byte[] ToBytes(this long thisLong)
{
byte[] bytes = new byte[8];
bytes[0] = (byte)(thisLong & 0xFF);
bytes[1] = (byte)((thisLong & 0xFF00) >> 8);
bytes[2] = (byte)((thisLong & 0xFF0000) >> 16);
bytes[3] = (byte)((thisLong & 0xFF000000) >> 24);
bytes[4] = (byte)((thisLong & 0xFF00000000) >> 32);
bytes[5] = (byte)((thisLong & 0xFF0000000000) >> 40);
bytes[6] = (byte)((thisLong & 0xFF000000000000) >> 48);
bytes[7] = (byte)((thisLong & 0xFF00000000000000) >> 56);
return bytes;
}
但在“ thisLong&0xFF00000000000000”的最后一行出现错误:
CS0019: Operator '&' cannot be applied to operands of type 'long' and 'ulong' (CS0019)
对于int来说可以正常工作,所以不会持续很长时间吗?:
public static byte[] ToBytes(this int thisInt)
{
byte[] bytes = new byte[4];
bytes[0] = (byte)(thisInt & 0xFF);
bytes[1] = (byte)((thisInt & 0xFF00) >> 8);
bytes[2] = (byte)((thisInt & 0xFF0000) >> 16);
bytes[3] = (byte)((thisInt & 0xFF000000) >> 24);
return bytes;
}
我不想使用BitConvertor来避免字节序问题。
我不想使用ulong,因为转换可能会失败。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)