Home > Net >  What is parallel library in C# .net for Longs.toByteArray from Java to .NET C#
What is parallel library in C# .net for Longs.toByteArray from Java to .NET C#

Time:04-22

What is parallel library in C# .net for Longs.toByteArray from Java(com.google.common.primitives.Longs) to .NET C#.

In Java its public static byte[] toByteArray(long value) Returns a big-endian representation of value in an 8-element byte array; equivalent to ByteBuffer.allocate(8).putLong(value).array(). For example, the input value 0x1213141516171819L would yield the byte array {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}.

But how to do same thing in .NET C#

CodePudding user response:

You probably look for:

long myLongValue = 12345;
var longBytes = System.BitConverter.GetBytes(myLongValue);

CodePudding user response:

I got the answer. It can achieved using following method

public static byte[] toByteArray(long value) {

           byte[] result = new byte[8];
           for (int i = 7; i >= 0; i--)
            {
                 result[i] = (byte)(value & 0xffL);
                 value >>= 8;
            }
           return result;
    }
  • Related