【问题标题】:Convert A to ASCII Int and add +1 so it gets "B"将 A 转换为 ASCII Int 并添加 +1 以得到“B”
【发布时间】:2016-01-19 12:07:58
【问题描述】:

我需要将int 转换为char 并将char 转换回int

我有一个"A",我希望它得到一个"B",所以我需要将"A" 转换为int,然后将1 添加到它,然后将其转换回@987654330 @。

伪代码:

char s = "A";
int i = s.toInt;
i + 1;
s = i.toChar;

【问题讨论】:

    标签: c# string ascii


    【解决方案1】:

    您可以简单地在其上使用++ 运算符:

    char s = 'A';
    s++;
    // s = 'B'
    

    【讨论】:

      【解决方案2】:

      您可以通过隐式转换来管理它:

      char s = 'A';
      int i = (int)s;
      i++;
      s = (char)i;
      

      【讨论】:

        【解决方案3】:

        由于charint 之间存在隐式对话,因此您可以轻松地将++ operator 用于字符。

        var A = 'A';
        Console.WriteLine(++A); // B
        

        请记住,char 已经是 2 字节数值。

        【讨论】:

          猜你喜欢
          • 2021-01-30
          • 2017-11-07
          • 2020-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-10
          相关资源
          最近更新 更多