如何获取给定ascii值的字符

时间:2011-01-10 16:08:37

标签: c#

如何获取给定ascii代码的ascii字符。

e.g。我正在寻找一种方法,给出代码65将返回“A”。

由于

9 个答案:

答案 0 :(得分:130)

你的意思是“A”(一个string)或'A'(一个char)?

int unicode = 65;
char character = (char) unicode;
string text = character.ToString();

请注意,我将Unicode而不是ASCII称为C#的本机字符编码;基本上每个char都是UTF-16代码点。

答案 1 :(得分:34)

 string c = Char.ConvertFromUtf32(65);

c将包含“A”

答案 2 :(得分:10)

这适用于我的代码。

string asciichar = (Convert.ToChar(65)).ToString();

返回:asciichar = 'A';

答案 3 :(得分:4)

有几种方法可以做到这一点。

使用char struct(to string and back)

string _stringOfA = char.ConvertFromUtf32(65);

int _asciiOfA = char.ConvertToUtf32("A", 0);

只需转换值(显示的字符串和字符串)

char _charA = (char)65;

string _stringA = ((char)65).ToString();

使用ASCIIEncoding。
这可以在循环中用于执行整个字节数组

var _bytearray = new byte[] { 65 };

ASCIIEncoding _asiiencode = new ASCIIEncoding();

string _alpha = _asiiencode .GetString(_newByte, 0, 1);

您可以覆盖类型转换器类,这将允许您对值进行一些奇特的验证:

var _converter = new ASCIIConverter();

string _stringA = (string)_converter.ConvertFrom(65);

int _intOfA = (int)_converter.ConvertTo("A", typeof(int));

这是班级:

public class ASCIIConverter : TypeConverter
{
    // Overrides the CanConvertFrom method of TypeConverter.
    // The ITypeDescriptorContext interface provides the context for the
    // conversion. Typically, this interface is used at design time to 
    // provide information about the design-time container.
    public override bool CanConvertFrom(ITypeDescriptorContext context,
       Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(int))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }


    // Overrides the ConvertFrom method of TypeConverter.
    public override object ConvertFrom(ITypeDescriptorContext context,
       CultureInfo culture, object value)
    {

        if (value is int)
        {
            //you can validate a range of int values here
            //for instance 
            //if (value >= 48 && value <= 57)
            //throw error
            //end if

            return char.ConvertFromUtf32(65);
        }
        return base.ConvertFrom(context, culture, value);
    }

    // Overrides the ConvertTo method of TypeConverter.
    public override object ConvertTo(ITypeDescriptorContext context,
       CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(int))
        {
            return char.ConvertToUtf32((string)value, 0);
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

答案 4 :(得分:1)

也可以用其他方式完成

byte[] pass_byte = Encoding.ASCII.GetBytes("your input value");

然后打印结果。使用foreach循环。

答案 5 :(得分:0)

抱歉,我不认识Java,但今晚我遇到了同样的问题,所以我写了这个(它在c#中)

public string IncrementString(string inboundString)    {
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(inboundString.ToArray);
bool incrementNext = false;

for (l = -(bytes.Count - 1); l <= 0; l++) {
    incrementNext = false;

    int bIndex = Math.Abs(l);
    int asciiVal = Conversion.Val(bytes(bIndex).ToString);

    asciiVal += 1;

    if (asciiVal > 57 & asciiVal < 65)
        asciiVal = 65;
    if (asciiVal > 90) {
        asciiVal = 48;
        incrementNext = true;
    }

    bytes(bIndex) = System.Text.Encoding.ASCII.GetBytes({ Strings.Chr(asciiVal) })(0);

    if (incrementNext == false)
        break; // TODO: might not be correct. Was : Exit For
}

inboundString = System.Text.Encoding.ASCII.GetString(bytes);

return inboundString;
}

答案 6 :(得分:0)

只需尝试:

int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("data is: {0}", Convert.ToChar(n));

答案 7 :(得分:-1)

我相信一个简单的演员可以工作

int ascii = (int) "A"

答案 8 :(得分:-1)

这是一个适用于所有256个字节的函数,并确保您将看到每个值的字符:

static char asciiSymbol( byte val )
{
    if( val < 32 ) return '.';  // Non-printable ASCII
    if( val < 127 ) return (char)val;   // Normal ASCII
    // Workaround the hole in Latin-1 code page
    if( val == 127 ) return '.';
    if( val < 0x90 ) return "€.‚ƒ„…†‡ˆ‰Š‹Œ.Ž."[ val & 0xF ];
    if( val < 0xA0 ) return ".‘’“”•–—˜™š›œ.žŸ"[ val & 0xF ];
    if( val == 0xAD ) return '.';   // Soft hyphen: this symbol is zero-width even in monospace fonts
    return (char)val;   // Normal Latin-1
}