Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i want to convert a character into an integer number for processing but it is not picking it
code
test = Convert.ToInt32(ch);
Console.WriteLine(test);
if (test >= 0 && test <= 9)
{
numst[num_count] = test;
Console.WriteLine(numst[num_count]);
num_count++;
}
test,num_count are integer,numst is a integer array and ch is a character
i want to check that if ch is a number then put it into the integer array
please help me about where i was wrong in logic
thank you
If ch is a numeric character then this doesn't do what you think it does:
Convert.ToInt32(ch);
For example, the integer value of the character '9' is 57. According to your if condition, the only characters you're expecting are essentially unprintable characters (NUL through TAB).
It sounds like you're looking for Char.GetNumericValue():
test = Char.GetNumericValue(ch);
If you want to test if a digit, use char.IsDigit(). To convert that character to an integer use char.GetNumericValue():
if (char.IsDigit(ch))
{
numst[num_count] = char.GetNumericValue(ch);
Console.WriteLine(numst[num_count]);
num_count++;
}
char.IsDigit() will return true for characters between '0' and '9', but false for e.g. '*'.
char.GetNumericValue() gives you the numeric value that was represented by the character. So a '9' is converted to 9.
Convert.ToInt32() instead converts the char's value to an int. The value of a character like '9' is 0x39 or 57.
there are 2 methods
1st method
if (char.IsDigit(ch))
{
numst[num_count] = char.GetNumericValue(ch);
Console.WriteLine(numst[num_count]);
num_count++;
}
2nd method
if (ch >= 48 && ch<=57)
{
numst[num_count] = char.GetNumericValue(ch);
Console.WriteLine(numst[num_count]);
num_count++;
}
both of them are working fine and have some results
thank you for every one help me
Related
This question already has answers here:
C#: Convert int to char, but in literal not unicode representation
(2 answers)
Closed 7 months ago.
char chr1 = '9';
int num = 9;
char chr2 = (char)num;
if(chr1 == chr2)
Console.WriteLine("It worked");
else
Console.WriteLine("It did not work");
Console.WriteLine(chr2.GetType().Name);
Console.Write(chr2);
I want to convert an int to a char and compare it with another char. When i ran this code the output is
It did not work
Char
So it converts int to a char successfully but chr2 has no value it prints nothing (but its move the cursor)
Hence it has no value if is not working but i didn't understand why chr2 has no value.
just use this
char chr2 = Convert.ToChar(num.ToString());
or thanks to #Charlieface
char chr2 = num.ToString()[0];
this is because
num=Convert.ToByte(9); //9 it is invisible ASCII code - horisontal tab
num=Convert.ToByte('9') //57 it is visible ASCII code '9'
I'm trying to count the number of times an int appears in a string
int count = numbers
.Where(x => x == '0')
.Count();
If I type in the literal number I want to check for as seen above '0' it works.
But I want to use this as a method where I can check for other digits. This, unfortunately, doesn't work when I convert an int to a char and insert the digit variable
char digit = Convert.ToChar(0);
int count= numbers
.Where(x => x == digit)
.Count();
What am I doing wrong?
Convert.ToChar(Int32) returns Unicode character equivalent to the value of passed int. For example it will convert 65 to A:
Console.WriteLine(Convert.ToChar(65)); // prints "A"
If I understand your requirement correctly you can call ToString and take first char of resulting string instead of Convert.ToChar(0):
char digit = 0.ToString()[0]
Or as this answer suggests:
char c = (char)(i + 48);
Convert.ToChar(0) converts you bit representation to the according to the character encoding table (e.g. ASCII) . 0 in this case is a Null character. Whereas bit representation of a '0' character is 48 according to the table
You can use this snippet to compare you int digit to the chars
string numbers = "33";
int digit = 3;
int count = numbers
// '0' -'0' (48 - 48) will give you 0 int value,
// '1' - '0' (49 - 48) will give you 1 etc.
.Where(x => x - '0' == digit)
.Count();
but keep in mind that you digit might be greater than 9, then it would take more than one character and this is going a bit different problem.
This question already has answers here:
char + char = int? Why?
(15 answers)
Closed 4 years ago.
This is something they included as part of my course, just wondering why it does this and what they were trying to show with it but can't seem to figure it out. Is it some sort of principle when trying to concatenate chars to numbers?
Am I right in assuming that 'z' is a char because it's in single quotes here?
Is it some sort of error because you shouldn't write stuff like this?
Thanks in advance!
z is char value, char is basically a number. z will be implicitly converted to int (z code is 122), that's why 3 + 'z' + 4 == 129. It will be converted to int because in statement 3 + 'z' 3 is int, so result of addition will be also int.
In C# char is a 16 bit numeric value which represents a unicode character. So in your case z is implicitly evaluated as 122. So 3 + 122 + 4 equals 129.
How to convert a byte to a char? I don't mean an ASCII representation.
I have a variable of type byte and want it as a character.
I want just following conversions from byte to char:
0 ->'0'
1 ->'1'
2 ->'2'
3 ->'3'
4 ->'4'
5 ->'5'
6 ->'6'
7 ->'7'
8 ->'8'
9 ->'9'
(char)1 and Convert.ToChar(1) do not work. They result in '' because they think 1 is the ASCII code.
the number .ToString();
one.ToString(); // one.ToString()[0] - first char -'1'
two.ToString(); // two.ToString()[0] - first char -'2'
Note that you can't really convert a byte to a char
char is one char while byte can even three digits value!
If you want to use LINQ and you're sure the byte won't exceed one digit(10+) you can use this:
number.ToString().Single();
Simply using variable.ToString() should be enough. If you want to get fancy, add the ASCII code of 0 to the variable before converting:
Convert.ToChar(variable + Convert.ToByte('0'));
Use this for conversion.
(char)(mybyte + 48);
where mybyte = 0 or 1 and so
OR
Convert.ToChar(1 + 48); // specific case for 1
While others have given solution i'll tell you why your (char)1 and Convert.ToChar(1) is not working.
When your convert byte 1 to char it takes that 1 as an ASCII value.
Now ASCII of 1 != 1.
Add 48 in it because ASCII of 1 == 1 + 48`. Similar cases for 0, 2 and so on.
Assume you have variable byte x;
Just use (char)(x + '0')
Use Convert.ToString() to perform this.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 14 years ago.
Improve this question
I was looking at the System.Net Namespace and it has an IPAddress instance you can use. This has a Parse method which you can use to parse a string into an IPInstance then use the Address property to give you the long value.
However...
The number returned is NOT the true conversion.
e.g. For IP 58.0.0.0 , the System.Net namespace gives me a value of 58...
When in fact, the integer value should be 973078528
Can someone please show me the correct code do convert this?
The formula should be.. (for ip 192.1.20.10).
192 * (256*256*256) + 1 * (256*256) + 20 * (256) + 10
The reason this formula is correct is that the number it returns you can use in a >= and <= query to determine an IP address that falls within a range.
The Address Property (of the IPAddress instance) does not calculate/return this. A bonus point for anyone that knows why the address property does not return what I think is the correct answer...
Other examples from other links did not work either.
Please see How to convert an IPv4 address into a integer in C#?
What you are seeing appears to be an endian problem.
As a generic function, if your IP address is A.B.C.D then the value you're after is:
A << 24 + (= A * 16777216)
B << 16 + (= B * 65536)
C << 8 + (= C * 256)
D
On little endian machines when the four-byte array ABCD is cast into an integer it comes out with A as the least significant byte instead of the most significant.
I don't write vb.net code, but it should be pretty trivial to knock out a function that'll do this.
You'll need to ensure that A - D are all in the 0 .. 255 range first, though!
How to convert an IPv4 address into a integer in C#?
example code is listed in the picked answer
e; f, b
This might work, will try it and see.
public double IPAddressToNumber(string IPaddress)
{
int i;
string [] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for(i = arrDec.Length - 1; i >= 0 ; i = i -1)
{
num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
}
return num;
}
}