我想收到符号的 ascii 值GS
。我知道他的 ascii 值是 29 - 但我的问题是如何在 C# 代码上编写它而无需只编写 29。有没有办法做到这一点,就像我想获得 ascii 一样'x'
?
非常感谢。
我想收到符号的 ascii 值GS
。我知道他的 ascii 值是 29 - 但我的问题是如何在 C# 代码上编写它而无需只编写 29。有没有办法做到这一点,就像我想获得 ascii 一样'x'
?
非常感谢。
我不认为你想做的事情是直接可能的。如果您已经将值存储在char
变量中,那么您可以直接转换它:
int value = (int) groupSeperator;
如果您只是在寻找一种更易读的方法来创建组分隔符变量,那么请在某处定义一个常量。我已经包含了一个完整的控制字符列表,以便将来复制粘贴给其他人。
public const char Null = (char)0;
public const char StartOfHeading = (char)1;
public const char StartOfText = (char)2;
public const char EndOfText = (char)3;
public const char EndOfTransmission = (char)4;
public const char Enquiry = (char)5;
public const char Acknowledge = (char)6;
public const char Bell = (char)7;
public const char Backspace = (char)8;
public const char HorizontalTab = (char)9;
public const char LineFeed = (char)10;
public const char VerticalTab = (char)11;
public const char FormFeed = (char)12;
public const char CarriageReturn = (char)13;
public const char ShiftOut = (char)14;
public const char ShiftIn = (char)15;
public const char DataLinkEscape = (char)16;
public const char DeviceControl1 = (char)17;
public const char DeviceControl2 = (char)18;
public const char DeviceControl3 = (char)19;
public const char DeviceControl4 = (char)20;
public const char NagativeAcknowledge = (char)21;
public const char SynchronousIdle = (char)22;
public const char EndOfTrasmissionBlock = (char)23;
public const char Cancel = (char)24;
public const char EndOfMedium = (char)25;
public const char Substitute = (char)26;
public const char Escape = (char)27;
public const char FileSeperator = (char)28;
public const char GroupSeperator = (char)29;
public const char RecordSeperator = (char)30;
public const char UnitSeperator = (char)31;
并int
在需要时将其转换为。
你最好的选择是创建一个静态类Ascii
,你把代码放在:
static public class Ascii {
...
public static char GS = 29;
...
}
然后,您可以将其称为char c = Ascii.GS;
这样,你只需要写一次,而且你不容易打错字。我在 .NET 中搜索过类似的东西,但它似乎不存在。
Cast the char to an int
int i = (int)'x';
The result will be the character code.
您将无法简单地将 GS 转换为 ascii 字符。您要么必须构建一个类并手动进行转换,要么已经存储了 ascii 值。以下适用于后一种情况。
//We have a string containing the special character. This is just an example, I imagine you're actually getting it from a delimited file or such.
string c = Char.ConvertFromUtf32(29);
//Convert it to Utf
int something = char.ConvertToUtf32(c, 0);