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.
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:
How to convert list of strings to doubles?
(5 answers)
Closed 3 years ago.
Is there an easy way to directly convert my list of string into double data type and get the average value.
Here is my current list result:
["6.3000e-009", "7.3319e-009", "7.8303e-009"]
expected result:
7.15E-09
Any suggestion/Comments TIA.
You can use Linq to get the average:
var avg = input.Select(double.Parse).Average();
you can use Linq to objects to do this quite easily
something like..(untested)
var result = yourArray.Select( s => Double.Parse(s) ).Average();
This question already has answers here:
double.Parse throw a System.FormatException
(3 answers)
Closed 4 years ago.
I am having this kind of error: Input string was not in a correct format
Looking from debug I can see that this is the line that is causing the error:
double num = Math.Round(double.Parse(value), 4);
I have made some researches but my piece of code looks like is correct, so I am still not getting where I am doing wrong.
Any idea?
You can use the following code to get the expected results without it throwing an error.
double number;
if (Double.TryParse(value, out number))
{
// If the value is parsed as double (is ok).
} else {
// If the value is not parsed as number (like an error).
}
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:
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.