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();
}
Related
This question already has answers here:
Convert string numbers to Array C#
(3 answers)
Closed 2 years ago.
for example suppose that I have a string like "231143" and I wanna convert it to an int[] array which I can easily access 2,3,1,1,4,3 as an int. What should I do?
Without any error checking you can do:
var value = "231143";
var array = value.Select(c => c - '0').ToArray();
This makes use of a trick whereby you can subtract '0' from the value of a single character holding a number to get its integer value.
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:
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 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;
}