开发者

How to convert a byte array to uint64 and back in C#?

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);
           开发者_StackOverflow中文版         }
                    else
                        sb.Append("0");
                }
                sb.Append(" ");
            }   
                  Console.WriteLine(sb.ToString());
                  Console.Read();
    }
}

}


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.)


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());
       }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜