【发布时间】:2020-11-23 03:22:47
【问题描述】:
如何获取给定 ascii 代码的 ascii 字符。
例如我正在寻找一种给定代码 65 将返回“A”的方法。
谢谢
【问题讨论】:
标签: c#
如何获取给定 ascii 代码的 ascii 字符。
例如我正在寻找一种给定代码 65 将返回“A”的方法。
谢谢
【问题讨论】:
标签: c#
您是指“A”(string)还是“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”
【讨论】:
这适用于我的代码。
string asciichar = (Convert.ToChar(65)).ToString();
返回:asciichar = 'A';
【讨论】:
有几种方法可以做到这一点。
使用 char 结构(字符串并再次返回)
string _stringOfA = char.ConvertFromUtf32(65);
int _asciiOfA = char.ConvertToUtf32("A", 0);
只需转换值(显示字符和字符串)
char _charA = (char)65;
string _stringA = ((char)65).ToString();
使用 ASCII 编码。
这可以在循环中用于执行整个字节数组
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
}
【讨论】:
也可以通过其他方式实现
byte[] pass_byte = Encoding.ASCII.GetBytes("your input value");
然后打印结果。通过使用foreach 循环。
【讨论】:
试试这个:
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("data is: {0}", Convert.ToChar(n));
【讨论】:
我相信一个简单的演员表可以工作
int ascii = (int) "A"
【讨论】:
string 转换为 int。如果它是'A',那么它会起作用,但演员表将是多余的,因为从char到int的隐式转换。
对不起,我不懂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;
}
【讨论】: