开发者

How to get character for a given ascii value

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

e.g. I'm looking for a method that开发者_Python百科 given the code 65 would return "A".

Thanks


Do you mean "A" (a string) or 'A' (a char)?

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

Note that I've referred to Unicode rather than ASCII as that's C#'s native character encoding; essentially each char is a UTF-16 code point.


 string c = Char.ConvertFromUtf32(65);

c will contain "A"


This works in my code.

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

Return: asciichar = 'A';


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


Here's a function that works for all 256 bytes, and ensures you'll see a character for each value:

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
}


It can also be done in some other manner

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

and then print result. by using foreach loop.


Simply Try this:

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


I believe a simple cast can work

int ascii = (int) "A"


Sorry I dont know Java, but I was faced with the same problem tonight, so I wrote this (it's in 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜