Konwertuj char na int w C#

Mam char w c#:

char foo = '2';

Teraz chcę dostać 2 do int. Uważam, że to nawrócenie.Toint32 zwraca rzeczywistą wartość dziesiętną znaku, a nie liczbę 2. Będzie działać:

int bar = Convert.ToInt32(new string(foo, 1));

Int.parse działa tylko na ciągach.

Czy w C# nie ma natywnej funkcji, która by przechodziła z znaku do int bez robienia z niego łańcucha znaków? Wiem, że to trywialne, ale wydaje się dziwne, że nie ma nic rodzimego, aby bezpośrednio dokonać konwersji.

 100
Author: Cœur, 2008-10-27

13 answers

Ciekawe odpowiedzi, ale doktorzy mówią inaczej:

Użyj metod GetNumericValue, aby Konwertuj obiekt Char, który reprezentuje Liczba do typu wartości liczbowej. Użycie Parse i TryParse do konwersji a znak w łańcuchu do Char obiekt. Użyj ToString do Konwersji Char obiekt do String obiektu.

Http://msdn.microsoft.com/en-us/library/system.char.aspx

 99
Author: Chad Grant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-05 14:16:21

To przekonwertuje go na int:

char foo = '2';
int bar = foo - '0';

To działa, ponieważ każdy znak jest wewnętrznie reprezentowany przez liczbę. Znaki " 0 " do " 9 "są reprezentowane przez kolejne liczby, więc znalezienie różnicy między znakami" 0 " i " 2 " skutkuje liczbą 2.

 109
Author: Jeremy Ruten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-27 04:57:25

Czy ktoś rozważał użycie int.Parse() i int.TryParse() w ten sposób

int bar = int.Parse(foo.ToString());

Even better like this

int bar;
if (!int.TryParse(foo.ToString(), out bar))
{
    //Do something to correct the problem
}
Jest o wiele bezpieczniejszy i mniej podatny na błędy]}
 61
Author: faulty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-27 05:39:56
char c = '1';
int i = (int)(c-'0');

I można z niej utworzyć statyczną metodę:

static int ToInt(this char c)
{
    return (int)(c - '0');
}
 24
Author: sontek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-27 04:52:06

Try This

char x = '9'; // '9' = ASCII 57

int b = x - '0'; //That is '9' - '0' = 57 - 48 = 9
 8
Author: RollerCosta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-10 11:50:39

Domyślnie używasz UNICODE, więc sugeruję użycie metody wadliwego

int bar = int.Parse(foo.ToString());

Chociaż wartości liczbowe pod są takie same dla cyfr i podstawowych znaków łacińskich.

 7
Author: Nikolay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-26 07:10:49

To konwertuje do liczby całkowitej i obsługuje unicode

CharUnicodeInfo.GetDecimalDigitValue('2')

Możesz przeczytać więcej tutaj .

 2
Author: Dan Friedman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-18 02:32:38
char foo = '2';
int bar = foo & 15;

Wartość binarna znaków ASCII 0-9 wynosi:

0   -   00110000

1   -   00110001

2   -   00110010

3   -   00110011

4   -   00110100

5   -   00110101

6   -   00110110

7   -   00110111

8   -   00111000

9   -   00111001

I jeśli weźmiesz w każdym z nich pierwsze 4 LSB (używając bitowego i z 8 ' b00001111, który równa się 15), otrzymasz rzeczywistą liczbę (0000 = 0,0001=1,0010=2,... )

 1
Author: Tomer Wolberg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-26 15:01:18

The real way is:

Int theNameOfYourInt = (int).Char.GetNumericValue (theNameOfYourChar);

"theNameOfYourInt" - int, na który chcesz przekształcić swój znak.

"theNameOfYourChar" - znak, którego chcesz użyć, więc zostanie przekształcony w int.

Zostaw Wszystko inne.

 1
Author: Normantas Stankevičius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-12 18:08:21

Porównanie niektórych metod na podstawie wyniku, gdy znak nie jest cyfrą ASCII:

char c = '\n';                              
Debug.Print($"{c & 15}");                   // 10
Debug.Print($"{c ^ 48}");                   // 58
Debug.Print($"{c - 48}");                   // -38
Debug.Print($"{(uint)c - 48}");             // 4294967258
Debug.Print($"{char.GetNumericValue(c)}");  // -1 
 0
Author: Slai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-14 21:54:30

Używam Compact Framework 3.5, a nie ma " char.Parse" metoda. Myślę, że nie jest źle używać klasy Convert. (Patrz CLR via C#, Jeffrey Richter)

char letterA = Convert.ToChar(65);
Console.WriteLine(letterA);
letterA = 'あ';
ushort valueA = Convert.ToUInt16(letterA);
Console.WriteLine(valueA);
char japaneseA = Convert.ToChar(valueA);
Console.WriteLine(japaneseA);

Działa z ASCII char lub Unicode char

 0
Author: antonio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-19 13:09:03

To mi pomogło:

int bar = int.Parse("" + foo);
 -5
Author: Renán Díaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-26 02:07:29

Widziałem wiele odpowiedzi, ale wydają mi się mylące. Nie możemy po prostu użyć odlewu typu.

Dla ex:-

int s;
char i= '2';
s = (int) i;
 -5
Author: Learner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-29 18:22:34