Convert 64 bits array into Int64 or ulong C# - c#

I have an int array of bits (length always 64) like:
1110000100000110111001000001110010011000110011111100001011100100
and I want to write it in one Int64 (or ulong?) variable. How to do it?
I tried to create a BitArray and then get int, but it throws System.ArgumentException, on CopyTo line:
private static Int64 GetIntFromBitArray(BitArray bitArray) {
var array = new Int64[1];
bitArray.CopyTo(array, 0);
return array[0];
}

That is because as mentioned in the documentation,
The specified array must be of a compatible type. Only bool, int, and byte types of arrays are supported.
So you could do something like this: (not tested)
private static long GetIntFromBitArray(BitArray bitArray)
{
var array = new byte[8];
bitArray.CopyTo(array, 0);
return BitConverter.ToInt64(array, 0);
}
Looking at the implementation of BitArray.CopyTo, it would be faster to copy the bits into an int[] (and then build the long from its two halves), that could look something like this: (also not tested)
private static long GetIntFromBitArray(BitArray bitArray)
{
var array = new int[2];
bitArray.CopyTo(array, 0);
return (uint)array[0] + ((long)(uint)array[1] << 32);
}
Casts to uint are to prevent sign-extension.

Related

Bytes sequence problem during Marshaling in C#

I want to use my laptop to communicate with MES(Manufacturing Execution System).
And when I serialized the data (struct type), something happen.
The code below is what I have done:
[StructLayout(LayoutKind.Sequential, Pack = 4)]
struct DataPackage
{
public int a;
public ushort b;
public byte c;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)] public string d;
}
class Program
{
static void Main(string[] args)
{
DataPackage pack1 = new DataPackage();
pack1.a = 0x33333301;
pack1.b = 200;
pack1.c = 21;
pack1.d = "hello";
byte[] pack1_serialized = getBytes(pack1);
Console.WriteLine(BitConverter.ToString(pack1_serialized));
byte[] getBytes(DataPackage str)
{
int size = Marshal.SizeOf(str);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(str, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
}
}
And here is the outcome:
I want the outcome to be like this:
33-33-33-01-00-C8-15-68-65-6C-6C-6F
So the questions are:
Why is the uint / ushort type data reverse after Marshalling?
Is there any other way that I can send the data in the sequence that I want ?
Why is the last word "o" in string "hello" disappear in the byte array ?
Thanks.
1 - Because your expected outcome is big endian, and your system appears to use little endian, so basically reversed order of bytes compared to what you expect.
2- Easiest way is to "convert" your numbers to big endian before marshalling (that is change them in a way which will produce desired result while converting them using little endian), for example like this:
static int ToBigEndianInt(int x) {
if (!BitConverter.IsLittleEndian)
return x; // already fine
var ar = BitConverter.GetBytes(x);
Array.Reverse(ar);
return BitConverter.ToInt32(ar, 0);
}
static ushort ToBigEndianShort(ushort x) {
if (!BitConverter.IsLittleEndian)
return x; // already fine
var ar = BitConverter.GetBytes(x);
Array.Reverse(ar);
return BitConverter.ToUInt16(ar, 0);
}
And then:
pack1.a = ToBigEndianInt(0x33333301);
pack1.b = ToBigEndianShort(200);
Note that this way of conversion is not very efficient and if you need more perfomance you can do this with some bit manipulations.
3 - Because string is null terminated, and this null terminator counts in SizeConst. Since you have it 5, there will be 4 characters of your string + 1 null terminator. Just increase SizeConst = 6 (that might add additional zeroes at the end because of Pack = 4).

Convert pair of values to byte[]

I need a quick way of converting a pair of integers to a byte array. If it were a single value, I could use the following:
private static unsafe byte[] ToBytes(int value)
{
var bytes = new byte[4];
fixed (byte* pointer = bytes)
{
*(int*)pointer = value;
}
return bytes;
}
However I can't quite figure out how you'd do the same for a pair of integers. The following which I tried doesn't work but should help to show what I'm aiming for
private static unsafe byte[] ToBytes(int value1, int value2)
{
var bytes = new byte[8];
fixed (byte* pointer = bytes)
{
*(int*)pointer = value1;
*(int*)pointer + 4 = value2;
}
return bytes;
}
(I'm aware I can do the same using BitConverter, but I want to compare the performance of the two)
Whether this will perform better than alternatives I don't know but the correct syntax is like this:
*((int)pointer + 1) = value2;
This:
*(int)pointer + 4 = value2;
gives an error because this: *(int)pointer is a variable reference and thus your code really looks like this:
i + 4 = value2;
which is not legal syntax in C#.
Instead encapsulate the addition to the address before dereferencing it into a variable reference (an lvalue if you want to google for these things):
*((int)pointer + 1)
+4 is also incorrect since the pointer is a "pointer to int", not a "pointer to byte", which means that +X is really +X * sizeof(int) in terms of byte-level addressing.
Your final method should look like this:
private static unsafe byte[] ToBytes(int value1, int value2)
{
var bytes = new byte[8];
fixed (byte* pointer = bytes)
{
*(int*)pointer = value1;
*((int*)pointer + 1) = value2;
}
return bytes;
}

Marshalling part of a struct

I've been trying to figure out whether it is possible to marshal a part of a struct into an array of bytes without any errors. The reason for this is because the part not going to be marshalled has a variable sized array. This is the the code I'm using.
C#:
public struct Random {
[MarshalAs(UnmanagedType.I4)]
public int a;
[MarshalAs(UnmanagedType.I4)]
public int b;
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_RECORD)]
public Random1[] r;
}
public struct Random1 {
[MarshalAs(UnmanagedType.I4)]
public int c;
}
private void Form1_Load(object sender, EventArgs e) {
int size = Marshal.SizeOf(typeof(Random));
int s = 4;
Random r = new Random();
Random r1 = new Random();
r.a = 1;
r.b = 5;
r.r = new Random1[2];
r.r[0].c = 10;
r.r[1].c = 12;
IntPtr p = Marshal.AllocHGlobal(size);
try {
byte[] arr = new byte[size];
Marshal.StructureToPtr(r, p, false);
Marshal.Copy(p, arr, 0, 8);
Marshal.FreeHGlobal(p);
p = Marshal.AllocHGlobal(size);
Marshal.Copy(arr, 0, p, 8);
r1 = (Random) Marshal.PtrToStructure(p, typeof(Random));
Marshal.FreeHGlobal(p);
p = IntPtr.Zero;
Debug.WriteLine(r1.a);
Debug.WriteLine(r1.b);
Debug.WriteLine(r1.r[0].c);
Debug.WriteLine(r1.r[1].c);
} finally {
if (p != IntPtr.Zero) {
Marshal.FreeHGlobal(p);
}
}
}
When I try this code it gives me an ArgumentException in the StructureToPtr. What am I doing wrong or can this be done? If not I've read something about using IntPtr. Could someone tell me how to do this?
I've been thinking long and hard about this one, and after trying several (very hackish) options, I have arrived at the conclusion that what you are doing is not possible.
First of all, it isn't possible to directly marshal an array of variable length, because no meaningful size offset can be computed by the Marshal, and therefore the memory layout can not be determined.
If the struct wouldn't contain any non-blittable, non-primitive types, you could theoretically do something like this:
byte[] buffer = new byte[8];
Marshal.Copy(new IntPtr(&r), buffer, 0, 8);
IntegersOnlyStruct partialStruct;
fixed (byte* b = buffer)
partialStruct = *(IntegersOnlyStruct*) b;
And obtain the two integers contained in the struct that way. However, since your Random1 is a non-primitive, non-blittable type this approach won't get you very far either.
The IntPtr approach to keep a pointer to a memory location where your Random1 struct is kept in memory is an alternative that could possibly work, but I haven't gone in-depth on that, yet.
I think you may want to re-think the way you approach this, as it's not possible in its current form. If anybody can prove otherwise, I'd be happy to see how it can be done.

Convert a struct to bytes, save to file and read back to struct by another application in C#

I tried writing structures to a binary file but I can't read them correctly.
This is my struct. It has a dynamic number of "values". If the number of values is 3, then GetSize() will return 8 + (8*3) = 32
[StructLayout (LayoutKind.Sequential)]
public struct Sample
{
public long timestamp;
public double[] values;
public int GetSize()
{
return sizeof(long) + sizeof(double) * values.Length;
}
}
First, I convert the structure to bytes by:
public static byte[] SampleToBytes(Sample samp)
{
int size = samp.GetSize();
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(samp, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
Then, I write the bytes using BinaryWriter, and exit.
When I have to run the program again and read the file I saved. I use BinaryReader. I get every 32 bytes from the file and convert each array of 32 bytes back to struct using:
public static Sample BytesToSample(byte[] arr)
{
Sample samp = new Sample();
IntPtr ptr = Marshal.AllocHGlobal(32);
Marshal.Copy(arr, 0, ptr, 32);
samp = (Sample)Marshal.PtrToStructure(ptr, samp.GetType());
Marshal.FreeHGlobal(ptr);
return samp;
}
However, a SafeArrayTypeMismatchException occurs at PtrToStructure().
Could anyone please tell me what I am doing wrong?
Thanks.
You do realize that double[], being an array type, is a reference type? The struct holds the long followed by a reference to the heap object.
That would be the reason why it doesn't work, I think.
I believe you should simply write the elements of your array to a binary writer.

c# cast byte[*,*,*] to byte[]

I have an object with a Property of type byte[,,*]
now i'd like to use System.Random::NextBytes() to fill this multidimensional array with random values.
NextBytes however takes an argument of byte[]
can i cast the multidimensional array somehow to the singledimensional one in order to pass it as an argument?
thanks!
You can't cast it, but you can copy the values quickly from a normal byte[] to a byte[,,] using Buffer.BlockCopy. So you'll have to allocate a normal byte array to start with, then copy the results over.
Sample:
using System;
class Test
{
static void Main()
{
Random rng = new Random();
byte[,,] y = new byte[2,2,2];
FillArray(y, rng);
foreach (byte b in y)
{
Console.WriteLine(b);
}
}
static void FillArray(byte[,,] array, Random rng)
{
byte[] tmp = new byte[array.Length];
rng.NextBytes(tmp);
Buffer.BlockCopy(tmp, 0, array, 0, tmp.Length);
}
}

Categories

Resources