简体   繁体   中英

Is there any way to get the ascii value of special symbols like 'GS' using c#?

I would like to receive the ascii value of the symbol GS . I know that his ascii value is 29 - but my question is how to write it on a C# code without need to write only 29. Is there any way to do this same as if I would like to get the ascii of 'x' ?

Thanks a-lot.

I don't think what you want to do is directly possible. If you have the value already stored in a char variable then you can just cast it directly:

int value = (int) groupSeperator;

If you are just looking for a more readable way to create a group separator variable, then define a constant somewhere. I've included a full list of the control characters for future copy-paste benefit to others.

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;

And cast it to an int when needed.

your best bet is to create a static class Ascii , where you put the codes in:

static public class Ascii {
    ...
    public static char GS = 29;
    ...
}

You can then refer to it as char c = Ascii.GS;

this way, you only have to write them once, and you don't make typos easily. I searched for something like this in .NET, but it doesn't seem to exist.

Cast the char to an int

int i = (int)'x';

The result will be the character code.

You won't be able to simply cast GS as an ascii character. You'll either have to build a class and manually do the conversion, or already have the ascii value stored. The following works for the latter instance.

    //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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM