Wie kann ich das ASCII-Zeichen eines bestimmten ASCII-Codes erhalten?
Ich suche beispielsweise nach einer Methode, bei der der Code 65 "A" zurückgibt.
Vielen Dank
Antworten:
Meinst du "A" (a string) oder "A" (a char)?
int unicode = 65;
char character = (char) unicode;
string text = character.ToString();
Beachten Sie, dass ich mich eher auf Unicode als auf ASCII bezogen habe, da dies die native Zeichencodierung von C # ist. Im Wesentlichen ist jeder charein UTF-16-Codepunkt.
string c = Char.ConvertFromUtf32(65);
c enthält "A"
Es gibt einige Möglichkeiten, dies zu tun.
Verwenden von char struct (zum String und wieder zurück)
string _stringOfA = char.ConvertFromUtf32(65);
int _asciiOfA = char.ConvertToUtf32("A", 0);
Einfach den Wert umwandeln (Zeichen und Zeichenfolge werden angezeigt)
char _charA = (char)65;
string _stringA = ((char)65).ToString();
Verwenden von ASCIIEncoding.
Dies kann in einer Schleife verwendet werden, um ein ganzes Array von Bytes zu erstellen
var _bytearray = new byte[] { 65 };
ASCIIEncoding _asiiencode = new ASCIIEncoding();
string _alpha = _asiiencode .GetString(_newByte, 0, 1);
Sie können die Typkonverterklasse überschreiben. Auf diese Weise können Sie die Werte auf ausgefallene Weise überprüfen:
var _converter = new ASCIIConverter();
string _stringA = (string)_converter.ConvertFrom(65);
int _intOfA = (int)_converter.ConvertTo("A", typeof(int));
Hier ist die Klasse:
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);
}
}
Dies kann auch auf andere Weise erfolgen
byte[] pass_byte = Encoding.ASCII.GetBytes("your input value");
und dann Ergebnis drucken. mit foreachSchleife.
Hier ist eine Funktion, die für alle 256 Bytes funktioniert und sicherstellt, dass für jeden Wert ein Zeichen angezeigt wird:
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
}
Ich glaube, eine einfache Besetzung kann funktionieren
int ascii = (int) "A"
stringnach konvertieren int. Wenn es 'A'so wäre, würde es funktionieren, aber die Besetzung wäre überflüssig, da es eine implizite Konvertierung von charnach gibt int.
Entschuldigung, ich kenne Java nicht, aber ich hatte heute Abend das gleiche Problem, also habe ich das geschrieben (es ist 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;
}