【发布时间】:2013-05-25 18:26:56
【问题描述】:
为什么这个程序不起作用?我将一个字节数组转换为长数组。然后从 long 我转换回字节数组。生成的字节数组与原始数组不同。
class Program
{
static void Main(string[] args)
{
byte[] myBytes = { 0, 0, 0, 32, 56, 99, 87, 34, 56, 56, 34, 33, 67
, 56, 66, 72, 1, 0, 0, 56, 0, 22};
long data = BitConverter.ToInt64(myBytes, 0);
byte[] byteData = BitConverter.GetBytes(data);
Console.WriteLine("byte array: " + BitConverter.ToString(myBytes));
Console.WriteLine("byte array: " + BitConverter.ToString(byteData));
}
}
【问题讨论】:
-
作为方法名称(
ToInt64) 意味着long的长度为 64 位(8 字节)。 -
它不是不工作,但没有像你预期的那样工作。
-
是的,你在那个数组中有 22 个字节,int64 是 8。你为什么要转换成 long 呢?
标签: c# bytearray long-integer bitconverter