How to get character for a given ascii value

后端 未结 9 2711
醉酒成梦
醉酒成梦 2020-11-29 05:51

How can I get the ascii character of a given ascii code.

e.g. I\'m looking for a method that given the code 65 would return \"A\".

Thanks

9条回答
  •  感情败类
    2020-11-29 06:04

    There are a few ways to do this.

    Using char struct (to string and back again)

    string _stringOfA = char.ConvertFromUtf32(65);
    
    int _asciiOfA = char.ConvertToUtf32("A", 0);
    

    Simply casting the value (char and string shown)

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

    Using ASCIIEncoding.
    This can be used in a loop to do a whole array of bytes

    var _bytearray = new byte[] { 65 };
    
    ASCIIEncoding _asiiencode = new ASCIIEncoding();
    
    string _alpha = _asiiencode .GetString(_newByte, 0, 1);
    

    You can override the type converter class, this would allow you to do some fancy validation of the values:

    var _converter = new ASCIIConverter();
    
    string _stringA = (string)_converter.ConvertFrom(65);
    
    int _intOfA = (int)_converter.ConvertTo("A", typeof(int));
    

    Here is the Class:

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

提交回复
热议问题