如何在 C# 中将字节数组转换为 uint64 并返回?

发布于 2024-12-06 14:13:38 字数 1987 浏览 1 评论 0 原文

我已经尝试这个很久了。我有一个字节数组,我想将其转换为 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();
    }
}

}

I have been trying this for long. I have a byte array, which I want to convert to ulong and return the value to another function and that function should get the byte values back.

I tried bitshifting, but it was unsuccessfull in few cases. Is there any alternate to bitshifting? or do you have any short example? Thanks for the help.

Here is the bitshift code that I used, I don't understant why the second entry is not 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();
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-12-13 14:13:38

首先,我会找出位移位出了什么问题,以防您再次需要它。它应该工作正常。

其次,还有一个替代方案 BitConverter.ToUInt64BitConverter.GetBytes(ulong) 如果您愿意使用系统字节序。

如果您希望能够指定字节顺序,我的 EndianBitConverter 类您可以使用的 >MiscUtil 库。

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

Firstly I'd work out what went wrong with bitshifting, in case you ever needed it again. It should work fine.

Secondly, there's an alternative with BitConverter.ToUInt64 and BitConverter.GetBytes(ulong) if you're happy using the system endianness.

If you want to be able to specify the endianness, I have an EndianBitConverter class in my MiscUtil library which you could use.

(If you just need it to be reversible on the same sort of machine, I'd stick with the built in one though.)

请你别敷衍 2024-12-13 14:13:38

我不确定您最初所做的左位移位和右位移位的意义是什么。(我假设您正在尝试生成 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());
       }

I'm not sure what the point of the left bitshifting and right bitshifting you're doing initially.(i'm assuming you're trying to generate Uint64 values to test your function with).
To fix your function, just cast the numbers to UInt64 and then test them. Alternatively you can create long literals by using a suffix of l. such as 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());
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文