如何在c#中将字节数组转换为uint64和返回



我已经尝试了很长时间。我有一个字节数组,我想将其转换为ulong并将值返回给另一个函数,该函数应该获得字节值。

我尝试过位移,但在少数情况下是不成功的。有替代位移的方法吗?或者你能举个简短的例子吗?谢谢你的帮助。

这是我使用的位移代码,我不明白为什么第二个条目不是00000001:

using System;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
         int[]responseBuffer = {0,1,2,3,4,5};

        UInt64 my = (UInt64)(((UInt64)(((responseBuffer[0]) << 40) & 0xFF0000000000)) |
              (UInt64)(((responseBuffer[1]) << 32) & 0x00FF00000000) |
              (UInt64)(((responseBuffer[2]) << 24) & 0x0000FF000000) |
              (UInt64)(((responseBuffer[3]) << 16) & 0x000000FF0000) |
               (UInt64)(((responseBuffer[4]) << 8) & 0x00000000FF00) | 
               (UInt64)(responseBuffer[5] & 0xFF));
         UInt64[] m_buffer = {(UInt64)((my >> 40) & 0xff),
                             (UInt64)((my >> 33) & 0xff) ,
                             (UInt64)((my >> 24) & 0xff) ,
                             (UInt64)((my>> 16) & 0xff) ,
                             (UInt64)((my>> 8) & 0xff) ,
                             (UInt64)(my& 0xff)};
         Console.WriteLine("" + m_buffer[1]);
            //string m_s = "";
            StringBuilder sb = new StringBuilder();
            for (int k = 0; k < 6; k++)
            {
                int value = (int)m_buffer[k];
                for (int i = 7; i >= 0; i--)
                {
                    if ((value >> i & 0x1) > 0)
                    {
                        sb.Append("1");
                        value &= (Byte)~(0x1 << i);
                    }
                    else
                        sb.Append("0");
                }
                sb.Append(" ");
            }   
                  Console.WriteLine(sb.ToString());
                  Console.Read();
    }
}

}

首先,我会找出位移出了什么问题,以防你再次需要它。应该没问题。

其次,如果您喜欢使用系统端序,可以选择BitConverter.ToUInt64BitConverter.GetBytes(ulong)

如果你想要能够指定端序,我有一个EndianBitConverter类在我的MiscUtil库,你可以使用。

(如果你只是需要它在同一类型的机器上是可逆的,我坚持使用内置的)

我不确定你最初做的左移位和右移位的意义是什么。(我假设你正在尝试生成Uint64值来测试你的函数)。要修复您的函数,只需将数字转换为UInt64,然后测试它们。或者,您可以通过使用l后缀来创建长文字。如UInt64[] responseBuffer = {0l,1l};

        static void Main(string[] args)
        {
            int[] responseBuffer = { 0, 1, 2, 3, 4, 5 };
            List<UInt64> bufferList = new List<ulong>();
            foreach (var r in responseBuffer)
                bufferList.Add((UInt64)r);
            UInt64[] m_buffer = bufferList.ToArray();
            foreach (var item in m_buffer)
                Console.WriteLine(item);
            //string m_s = "";
            StringBuilder sb = new StringBuilder();
            for (int k = 0; k < m_buffer.Length; k++)
            {
                int value = (int)m_buffer[k];
                for (int i = 7; i >= 0; i--)
                {
                    if ((value >> i & 0x1) > 0)
                    {
                        sb.Append("1");
                        value &= (Byte)~(0x1 << i);
                    }
                    else
                        sb.Append("0");
                }
                sb.Append(" ");
            }
            Console.WriteLine(sb.ToString());
       }

最新更新