如何获得给定ascii代码的ascii字符。
例如,我正在寻找一种方法,该方法给出的代码65将返回“ A”。
谢谢
Answers:
您是指“ A”(a string)还是“ A”(a char)?
int unicode = 65;
char character = (char) unicode;
string text = character.ToString();
注意,我指的是Unicode而不是ASCII,因为这是C#的本机字符编码。本质上每个char都是一个UTF-16代码点。
string c = Char.ConvertFromUtf32(65);
c将包含“ A”
有几种方法可以做到这一点。
使用char struct(以字符串形式返回)
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);
}
}
这是一个适用于所有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
}
我相信一个简单的演员表就能奏效
int ascii = (int) "A"
string为int。如果是'A'这样,它将起作用,但是由于存在从到的隐式转换,因此强制转换将是多余的。charint
抱歉,我不懂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;
}