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
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);
}
}
This works in my code.
string asciichar = (Convert.ToChar(65)).ToString();
Return: asciichar = 'A';
string c = Char.ConvertFromUtf32(65);
c will contain "A"
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.
I believe a simple cast can work
int ascii = (int) "A"
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
}