Single quoted string to uint in c# [duplicate] - c#

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

Related

How can I convert a string to an int array in C#? [duplicate]

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.

I can't properly convert string to Int in C# [duplicate]

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.

Double.TryParse doesn't allow to use divisions (fractions) as input [duplicate]

This question already has answers here:
Convert a text fraction to a decimal
(5 answers)
Parse Math Expression [duplicate]
(9 answers)
Closed 3 years ago.
I have created a method that splits a string a^b and converts a and b to doubles. However, if I input the value of either a or b using a fraction (for example 3/5 instead of 0.6) the method doesn't work; it only allows me to input it like 0.6. Why is this, and is it possible to fix it?
The code is shown below:
public static double Coefficient()
{
while (true)
{
string input = Console.ReadLine();
string[] items = input.Split('^');
if (items.Length == 1)
{
if (double.TryParse(items[0], out double A))
return A;
}
else if (items.Length == 2)
{
if (double.TryParse(items[0], out double A) &
double.TryParse(items[1], out double B))
return Math.Pow(A, B);
}
Console.WriteLine("\nPlease follow the specified input form.");
}
}
You're trying to parse expressions with methods that only know how to handle specifically formatted numbers as inputs. You need to either write an equation parser or split your inputs appropriately. You are already doing this by splitting ^ - you can do the same with /.
In fact, there are libraries that already do this.

Getting unsigned short binary data from integer in C# [duplicate]

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

Easy way to get hex from a string in .net [duplicate]

This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
Closed 8 years ago.
I want to get hex from a string like this:
Color c = new Color();
c.A = Int32.Parse("0x7F");
What's the right method for this ?
If the hex string isn't known till runtime, then something like this:
c.A = Convert.ToInt32("0x7F", 16);
Or as a literal if the value is known at compile time:
c.A = 0x7F;
With the Parse method, check the overload which allows NumberStyles. Reference http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx
int bla = Int32.Parse("beef", NumberStyles.HexNumber);
Convert.ToInt32("hexvaluestring", 16);
Should be enough.

Categories