【问题标题】:Is there a way to convert Char into Int without using numbers?有没有办法在不使用数字的情况下将 Char 转换为 Int?
【发布时间】:2019-08-27 07:13:16
【问题描述】:

标题说明了一切。我正在尝试在 Visual Studio 中将 char 转换为 int。

我已经试过了:

int a;
a = (int)x;
System.Console.WriteLine(a);

但除此之外它没有给我任何东西(试图理解代码): 114117105

【问题讨论】:

  • 什么是x?你能提供一个合适的minimal reproducible example吗?
  • "标题说明了一切" 不是真的,尤其不是x 在执行代码时的实际情况。
  • char x = "kajdgysf"不是一个字符
  • @RuiPedro 你可以edit你的问题 - 请这样做。 char x = "kajdgysf" 不是有效的 C# 并且无法编译。
  • 即使您的输入 一个符号字符 - 它显然不是 - 您如何期望 "kajdgysf" 转换为数字?您的问题仍然完全不清楚。

标签: c# char int


【解决方案1】:

这样就可以了:

//a char is a 16-bit numerical value
//usually used for the representation of characters
//here I assign 'a' to it; keep in mind; 'a' has also a numeric representation
//i.e.: 97
char x = 'a';

int a;

//the `int a` is an integer and, through a cast, receives the numeric value
//besides the bit-width (debatable) the data contents are the same.
//If we assign it to an `int` only the "purpose" c.q. representation will change
a = (int)x;

//since we put in an `int` a number will be shown (because of it's purpose)
//if we put in x, then `a` will be shown.
System.Console.WriteLine(a);

输出

97

正如您现在所了解的那样; stringarraychars。 因此,字符串很难用单个数字表示,因为它是二维的。

相当于说,将0,4,43434,878728,3477,3.14159265 转换为单个数字。

https://dotnetfiddle.net/qSYUdP

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char

为什么a 的输出是97;您可以在字符表中查找它,例如:ascii

请注意,实际输出的字符由所选字体/字符表决定。对于大多数字体,ASCII 已实现,但不能保证。所以,97 不会总是产生a

【讨论】:

  • 我不知道为什么会这样,但是谢谢。
  • 我的意思是这封信。为什么结果是 97?
  • @RuiPedro 因为二进制数 97 代表 ascii 字符“a”。在这里查找:asciitable.com
猜你喜欢
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多