This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
What does “Invalid managed/unmanaged type combination.” mean?
how we will code these structures(Written in C++) in C#
typedef struct tagBIRDMATRIX
{
short n[3][3]; // array of matrix elements
}BIRDMATRIX;
The size should be the number of elements in your cross product.
struct BIRDMATRIX
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
short[,] n;
}
Related
This question already has answers here:
C# converting string to int goes wrong
(5 answers)
Closed 3 years ago.
The type conversion doesn't function as expected
I broke down the problem but it still occurs
string a = "123";
int i = Convert.ToInt32(a[0]);
Console.WriteLine(i);
I expect the result of 1 but I get 49. I can't imagine how.
When you do the indexer a[0] you get a char which for 1 is char code number 49. Do Convert.ToInt32(a[0].ToString()) or subtract 48 from the result you get instead to get the numerical representation.
This question already has answers here:
easy and fast way to convert an int to binary?
(5 answers)
Closed 5 years ago.
How to get unsigned short (always 16 bit, machine byte order) binary string in c# from integer?
Number can be from 1 to 65000.
I could not get exactly what you need but a simple cast could do this.
static void Main()
{
int i = 50;
ushort short_val = (ushort)i;
uint uint_val = (uint)i;
Console.WriteLine(uint_val);
Console.ReadLine();
}
This question already has answers here:
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 9 years ago.
I was translating some C++ to C# code and I saw the below definiton:
#define x 'liaM'
First, what does this single quoted constant mean? Do I make it a string constant in c#?
Second, this constant is assigned as value to a uint variable in C++. How does that work?
uint m = x;
This is sometimes called a FOURCC. There's a Windows API that can convert from a string into a FOURCC called mmioStringToFOURCC and here's some C# code to do the same thing:
public static int ChunkIdentifierToInt32(string s)
{
if (s.Length != 4) throw new ArgumentException("Must be a four character string");
var bytes = Encoding.UTF8.GetBytes(s);
if (bytes.Length != 4) throw new ArgumentException("Must encode to exactly four bytes");
return BitConverter.ToInt32(bytes, 0);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# dynamic operator
I don't know if this is possible but let me ask:
For example I generate a simple math operation from a list
such as
List lstMat={=,+,-}
Then I generate a random value between 0-2 and select that operator from that list
such as
int ir1=1;
int ir2=2;
int irNew= ir1 lstMat[1] ir2 ;
//irNew would be 3
Is this possible?
The closest thing I can think of
List<Func<int, int, int>> lstMat = new List<Func<int, int, int>>()
{
(x,y)=>x.CompareTo(y),
(x,y)=>x+y,
(x,y)=>x-y
};
int ir1=1;
int ir2=2;
int irNew= lstMat[1](ir1,ir2);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C#: Any faster way of copying arrays?
I have an array of structs like this:
struct S
{
public long A;
public long B;
}
...
S[] s1 = new S[1000000];
...
S[] = new S[s1.Length];
// Need to create a copy here.
I can use unsafe mode and copy the source array of structs to a byte array and then from byte array to destination array of structs. But that means I will have to allocate a huge intermediate byte array. Is there a way to avoid this? Is it possible to somehow represent a destination array as a byte array and copy directly there?
unsafe
{
int size = Marshal.SizeOf(s0[0]) * s0.Length;
byte[] tmp = new byte[size];
fixed (var tmpSrc = &s0[0])
{
IntPtr src = (IntPtr)tmpSrc;
Marchal.Copy(tmpSrc, 0, tmp, 0, size);
}
// The same way copy to destination s1 array...
}
In case of Buffer.BlockCopy, it copies bytes[] to byte[] and not logical elements in array.
But this is really dependent on case to case.
Please test your code with Array.Copy first and see.