简体   繁体   English

如何将字符串转换为 ASCII

[英]How to convert a string to ASCII

How do I convert each letter in a string to its ASCII character value?如何将字符串中的每个字母转换为其 ASCII 字符值?

.NET stores all strings as a sequence of UTF-16 code units. .NET 将所有字符串存储为一系列 UTF-16 代码单元。 (This is close enough to "Unicode characters" for most purposes.) (对于大多数用途,这与“Unicode 字符”足够接近。)

Fortunately for you, Unicode was designed such that ASCII values map to the same number in Unicode, so after you've converted each character to an integer, you can just check whether it's in the ASCII range.对您来说幸运的是,Unicode 的设计使得 ASCII 值映射到 Unicode 中的相同数字,因此在您将每个字符转换为整数后,您只需检查它是否在 ASCII 范围内。 Note that you can use an implicit conversion from char to int - there's no need to call a conversion method:请注意,您可以使用从charint的隐式转换 - 无需调用转换方法:

string text = "Here's some text including a \u00ff non-ASCII character";
foreach (char c in text)
{
    int unicode = c;
    Console.WriteLine(unicode < 128 ? "ASCII: {0}" : "Non-ASCII: {0}", unicode);
}

For Any String try this:对于任何字符串试试这个:

string s = Console.ReadLine();
foreach( char c in s)
{
    Console.WriteLine(System.Convert.ToInt32(c));
}
Console.ReadKey();

It's pretty simple: 这很简单:

string s = "ABCD";
byte[] bytes = Encoding.ASCII.GetBytes(s);
int result = BitConverter.ToInt32(bytes, 0);

Now you have an array for all bytes and encoding int is result . 现在你有一个数组用于所有字节,编码intresult If you need to go back, you can go as: 如果你需要回去,你可以去:

int i = result;
byte[] bytes2 = BitConverter.GetBytes(i);
string s2 = Encoding.ASCII.GetString(bytes);
byte[] bytes = Encoding.ASCII.GetBytes(inputString);

In the ASCII enconding each char is represented with 1 byte, however in C# the string type uses 2 bytes per char.在 ASCII 编码中,每个字符用 1 个字节表示,但是在 C# 中,字符串类型每个字符使用 2 个字节。

Hence, if you want to keep an ASCII "string" in memory, the most efficient way is to use a byte array.因此,如果您想在 memory 中保留一个 ASCII“字符串”,最有效的方法是使用字节数组。 You can always convert it back to string, but remember it will double in size in memory!你总是可以将它转换回字符串,但记住它在内存中的大小会加倍!

string value = new ASCIIEncoding().GetString(bytes);

Try Linq:试试林克:

Result = string.Join("", input.ToCharArray().Where(x=> ((int)x) < 127));

This will filter out all non ascii characters.这将过滤掉所有非 ascii 字符。 Now if you want an equivalent, try the following:现在,如果您想要等效的,请尝试以下操作:

Result = string.Join("", System.Text.Encoding.ASCII.GetChars(System.Text.Encoding.ASCII.GetBytes(input.ToCharArray())));

我认为此代码可能对您有所帮助:

string str = char.ConvertFromUtf32(65)

Use Convert.ToInt32() for conversion.使用 Convert.ToInt32() 进行转换。 You can have a look at How to convert string to ASCII value in C# and ASCII values .您可以查看如何将字符串转换为 C#ASCII 值中的 ASCII 值

You can do it by using LINQ-expression.您可以使用 LINQ 表达式来完成。

  public static List<int> StringToAscii(string value)
  {
        if (string.IsNullOrEmpty(value))
            throw new ArgumentException("Value cannot be null or empty.", nameof(value));

        return value.Select(System.Convert.ToInt32).ToList();
  } 

Here is an extension method based on Jon Skeet's answer:这是基于 Jon Skeet 回答的扩展方法:

    public static string ConvertUnicodeStringToAscii(this string text)
    {
        var sb = new StringBuilder();
        foreach (char c in text)
        {
            int unicode = c;
            if (unicode < 128)
            {
                sb.Append(c);
            }
        }
        return sb.ToString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM