Làm cách nào để lấy ký tự ascii của một mã ascii nhất định.
ví dụ: Tôi đang tìm một phương pháp cho mã 65 sẽ trả về "A".
Cảm ơn
Câu trả lời:
Ý bạn là "A" (a string) hay 'A' (a char)?
int unicode = 65;
char character = (char) unicode;
string text = character.ToString();
Lưu ý rằng tôi đã đề cập đến Unicode chứ không phải ASCII vì đó là mã hóa ký tự gốc của C #; về cơ bản mỗi điểm charlà một điểm mã UTF-16.
string c = Char.ConvertFromUtf32(65);
c sẽ chứa "A"
Có một số cách để làm điều này.
Sử dụng cấu trúc char (để xâu chuỗi và quay lại một lần nữa)
string _stringOfA = char.ConvertFromUtf32(65);
int _asciiOfA = char.ConvertToUtf32("A", 0);
Chỉ cần truyền giá trị (ký tự và chuỗi hiển thị)
char _charA = (char)65;
string _stringA = ((char)65).ToString();
Sử dụng ASCIIEncoding.
Điều này có thể được sử dụng trong một vòng lặp để thực hiện toàn bộ một mảng byte
var _bytearray = new byte[] { 65 };
ASCIIEncoding _asiiencode = new ASCIIEncoding();
string _alpha = _asiiencode .GetString(_newByte, 0, 1);
Bạn có thể ghi đè lớp trình chuyển đổi kiểu, điều này sẽ cho phép bạn thực hiện một số xác thực ưa thích của các giá trị:
var _converter = new ASCIIConverter();
string _stringA = (string)_converter.ConvertFrom(65);
int _intOfA = (int)_converter.ConvertTo("A", typeof(int));
Đây là Lớp:
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);
}
}
Nó cũng có thể được thực hiện theo một số cách khác
byte[] pass_byte = Encoding.ASCII.GetBytes("your input value");
và sau đó in kết quả. bằng cách sử dụng foreachvòng lặp.
Đây là một hàm hoạt động cho tất cả 256 byte và đảm bảo bạn sẽ thấy một ký tự cho mỗi giá trị:
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
}
Chỉ cần thử điều này:
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("data is: {0}", Convert.ToChar(n));
Tôi tin rằng một dàn diễn viên đơn giản có thể hoạt động
int ascii = (int) "A"
stringsang int. Nếu đúng 'A'thì nó sẽ hoạt động, nhưng dàn diễn viên sẽ dư thừa vì có một sự chuyển đổi ngầm từ charthành int.
Xin lỗi, tôi không biết Java, nhưng tôi đã gặp phải vấn đề tương tự tối nay, vì vậy tôi đã viết bài này (nó bằng 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;
}