将ASCII字符串转换为普通字符串C# [英] Converting a string of ASCII into normal string C#

查看:559
本文介绍了将ASCII字符串转换为普通字符串C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
.NET从十六进制值的字符串转换转换为Unicode字符(支持不同的代码页)

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

试图将包含ASCII字符串的字符串转换为文本,我似乎只能找到从Byte []转换的System.Text.ASCIIEncoding.ASCII.GetString,但是在这种情况下,我希望能够从字符串中做到这一点.

Looking to convert a string containing an ASCII string into text, i seem to be only be able to find System.Text.ASCIIEncoding.ASCII.GetString which converts from a Byte[] but in this circumstance I would like to be able to do it from a string.

its a string containing ASCII hex: For example : ASCI = 47726168616D would equal Graham

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

Are there any built in functions for this? help would be appreciated, thank you.

推荐答案

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);
}

这篇关于将ASCII字符串转换为普通字符串C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆