Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#int64列表到字节数组,反之亦然?_C#_Arrays_List_Casting_Arraylist - Fatal编程技术网

C#int64列表到字节数组,反之亦然?

C#int64列表到字节数组,反之亦然?,c#,arrays,list,casting,arraylist,C#,Arrays,List,Casting,Arraylist,请向我展示铸件的优化解决方案: (一) 及 谢谢你的帮助 使用。此库具有与大多数基元类型之间的转换器,用于大端、小端和主机顺序字节排序。编辑,修复了正确的8字节转换,在转换回字节数组时效率也不是很高 public static List<Int64> ToList(byte[] bytes) { var list = new List<Int64>(); for (int i = 0; i < bytes.Length

请向我展示铸件的优化解决方案:

(一)


谢谢你的帮助

使用。此库具有与大多数基元类型之间的转换器,用于大端、小端和主机顺序字节排序。

编辑,修复了正确的8字节转换,在转换回字节数组时效率也不是很高

    public static List<Int64> ToList(byte[] bytes)
    {
        var list = new List<Int64>();
        for (int i = 0; i < bytes.Length; i += sizeof(Int64))
            list.Add(BitConverter.ToInt64(bytes, i));

        return list;
    }

    public static byte[] ToBytes(List<Int64> list)
    {
      var byteList = list.ConvertAll(new Converter<Int64, byte[]>(Int64Converter));
      List<byte> resultList = new List<byte>();

      byteList.ForEach(x => { resultList.AddRange(x); });
      return resultList.ToArray();
    }

    public static byte[] Int64Converter(Int64 x)
    {
        return BitConverter.GetBytes(x);
    }
公共静态列表列表(字节[]字节)
{
var list=新列表();
for(inti=0;i{resultList.AddRange(x);});
返回resultList.ToArray();
}
公共静态字节[]Int64转换器(Int64 x)
{
返回BitConverter.GetBytes(x);
}

CLR数组知道它们的类型和大小,因此不能将一种类型的数组强制转换为另一种类型的数组。但是,可能会对值类型进行不安全的强制转换。例如,下面是
位转换器.GetBytes(long)
的源代码:

您可以将其写入一个长列表,如下所示:

public static unsafe byte[] GetBytes(IList<long> value)
{
    byte[] buffer = new byte[8 * value.Count];
    fixed (byte* numRef = buffer)
    {
        for (int i = 0; i < value.Count; i++)
            *((long*) (numRef + i * 8)) = value[i];
    }
    return buffer;
}
公共静态不安全字节[]GetBytes(IList值)
{
字节[]缓冲区=新字节[8*value.Count];
固定(字节*numRef=缓冲区)
{
for(int i=0;i

当然,如果你想这样做的话,那么你很容易就会反其道而行之。

你想把每个字节转换成Int64(反之亦然)还是8个字节的块?(sizeof Int64)@BrokenGlass我需要将每8个字节(分块)转换为一个Int64值,反之亦然;)你想要的是
BitConverter
解决方案还是
memcpy
解决方案?@Gabe如果是在C++上,我最想要的是cast:(u int8*)ptrInt64;(uu int64*)ptrInt8;感谢您的帮助和代码!我需要将每个8字节转换为一个Int64值,反之亦然;)请注意,这始终转换为主机字节顺序或从主机字节顺序转换为主机字节顺序。如果您使用这些函数序列化数据或以其他方式打包数据以便在计算机之间传输,则应选择特定的字节顺序(大或小的尾端),并专门使用它。否则,当您的软件在具有相反字节顺序的平台上运行时,您将遇到麻烦。cdhowie:关于字节顺序,您完全正确,但OP询问如何转换数组,这意味着他只关心主机的字节顺序。
__int64* ptrInt64 = (__int64*)ptrInt8;
__int8* ptrInt8 = (__int8*)ptrInt64
    public static List<Int64> ToList(byte[] bytes)
    {
        var list = new List<Int64>();
        for (int i = 0; i < bytes.Length; i += sizeof(Int64))
            list.Add(BitConverter.ToInt64(bytes, i));

        return list;
    }

    public static byte[] ToBytes(List<Int64> list)
    {
      var byteList = list.ConvertAll(new Converter<Int64, byte[]>(Int64Converter));
      List<byte> resultList = new List<byte>();

      byteList.ForEach(x => { resultList.AddRange(x); });
      return resultList.ToArray();
    }

    public static byte[] Int64Converter(Int64 x)
    {
        return BitConverter.GetBytes(x);
    }
public static unsafe byte[] GetBytes(long value)
{
    byte[] buffer = new byte[8];
    fixed (byte* numRef = buffer)
    {
        *((long*) numRef) = value;
    }
    return buffer;
}
public static unsafe byte[] GetBytes(IList<long> value)
{
    byte[] buffer = new byte[8 * value.Count];
    fixed (byte* numRef = buffer)
    {
        for (int i = 0; i < value.Count; i++)
            *((long*) (numRef + i * 8)) = value[i];
    }
    return buffer;
}