c# - 将ASCII字符串转换为普通字符串C#

标签 c# ascii

<分区>

Possible Duplicate:
.NET Convert from string of Hex values into Unicode characters (Support different code pages)

想要将包含 ASCII 字符串的字符串转换为文本,我似乎只能找到从 Byte[] 转换而来的 System.Text.ASCIIEncoding.ASCII.GetString 但在这种情况下我希望能够从一个字符串来做。

它是一个包含 ASCII 十六进制的字符串:例如:ASCI = 47726168616D 等于 Graham

是否有任何内置函数?帮助将不胜感激,谢谢。

最佳答案

private static string GetStringFromAsciiHex(String input)
{
    if (input.Length % 2 != 0)
        throw new ArgumentException("input");

    byte[] bytes = new byte[input.Length / 2];

    for (int i = 0; i < input.Length; i += 2)
    {
        // Split the string into two-bytes strings which represent a hexadecimal value, and convert each value to a byte
        String hex = input.Substring(i, 2);
        bytes[i/2] = Convert.ToByte(hex, 16);                
    }

    return System.Text.ASCIIEncoding.ASCII.GetString(bytes);
}

关于c# - 将ASCII字符串转换为普通字符串C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10349753/

相关文章:

c# - 更改某些字符的颜色并在 RichTextBox 中计数

c# - 某些应用程序是否可能打开/关闭 iis 托管的 Web 服务?

c# - Clearquest 查询

scala - 如何使用 Scala 将字符串转换为 ASCII

string - 是否存在unicode "New-Line"(换行符),就像unicode `space`( )一样?

ascii - 不使用 ctype.h 转换小写/大写字母

html - $ (&dollar;) 的命名 html 实体是怎么回事

c# - 如何仅在 XAML 中设置上边距?

c# - 使用序列化检查对象的状态变化?

windows - 批处理文件中的国际字符