【问题标题】:I want to have ASCII output just with char array in C#我想在 C# 中使用 char 数组输出 ASCII
【发布时间】:2021-01-16 02:45:58
【问题描述】:

大家好,我需要你的帮助;

char[] cha = new char[] 
        {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

我做了这个数组。

但是

foreach (char a in cha)
        {
            Console.WriteLine(a);
        }

当我使用此代码时,输​​出只是 a、b、c、d....z

但是当我使用这段代码时

foreach (char a in cha)
        {
            Console.WriteLine(a + g);
        }

输出是 ASCII 数字。 我想要没有 g 的 ASCII 输出 我该怎么办?

谢谢。

【问题讨论】:

  • Console.WriteLine((int)a);
  • 好吧,char 只不过是一个数字。因此,执行+ 将添加这些数字。
  • @HimBromBeere 哦..我明白了。谢谢!

标签: c# for-loop foreach char ascii


【解决方案1】:

你可以使用Encoding.ASCII.GetBytes:

var cha = new[] {'a'};
var bytes = Encoding.ASCII.GetBytes(cha);

你也可以使用Convert.ToInt32:

int x = Convert.ToInt32('a')

或者只是明确地转换为int:

int x = (int)'a';

【讨论】:

  • 我喜欢 Cover.ToInt32。我现在就试试你的答案谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多