ENUM enumeration type as you want to do

xiaoxiao2021-03-05  25

Simplely written a Helper that operates an ENUM enumeration type, which is implemented as follows: 1, the Enum object value 2 can be obtained by the int value, and the String value can be obtained by the ENUM object value 3, and the HEX value can be obtained by ENUM object value 4, Hex , ASCII value <- get-> enum object value

As for its application, the first is to operate when it is convenient to write the program; followed by simplifying the protocol characters, but it means clearly what ENUM represents each ASCII represented when writing programs; Compressed strings on the UDP transport protocol.

Using system;

Namespace hxh.collections

{

Public Enum EXAMPLENORMALENUM

{

ONLINE = 1, //

1

i

N DB

OFFLINE = 2, //

2

i

N DB

HIDE = 3, //

3

i

N DB

Busy = 4, //

4

i

N DB

Away = 5, //

5

i

N DB

Other = 6, //

6

i

N DB

}

Public Enum EXAMPLEHEXENUM

{

Login = 0x22, // Login Server ASCII = "

Logoff = 0x23, // Exit server ASCII = #

Online = 0x24, // Online ASCII = $

OFFLINE = 0x25, // Offline ASCII =%

Away = 0x26, // Leave ASCII = &

Busy = 0x27, // Busy ASCII = '

HIDE = 0x28, // stealth ASCII = (

}

///

/// ENumHelper's summary description.

///

Public Class Enumhelper

{

Public enumhelper ()

{

}

///

/// Remove one INT value from the enum, convert it into an enumeration type value

///

///

///

///

/// Examplenormalenum Status = (ExampleEnormalenum) ENUMHELPER.INTVALUETOEENUM (TypeOf (ExampleEnormalenum), 1); Get value of Examplenormalenum.online

Public Static Object INTVALUETOENUM (System.Type Protocoltype, int enjoy) {

Object myObject = enum.parse (Protocoltype, Enum.getName (protocoltype, enumintvalue);

Return myObject;

}

///

/// A string value is used anywhere from the enum, convert it into an enumerated type value.

///

///

///

///

/// ExampleNormalenum Status = (ExampleEnormalenum) Enumhelper.stringValuetoEnum (TypeOf (ExampleEnormalenum), "Offline"); Get the value of Examplenormalenum.Offline

Public Static Object StringvaluetoEnum (System.Type Protocoltype, String EnumstringValue)

{

Object myObject = enum.parse (Protocoltype, EnumstringValue, true);

Return myObject;

}

///

// get all INT values ​​in an ENUM

///

///

///

Public static int [] getenumintvalues ​​(System.Type Protocoltype)

{

int [] MyintArray = new int [enum.getvalues ​​(protocoltype) .length];

Array.copy (enum.getvalues ​​(protocoltype), myintarray, enum.getvalues ​​(protocoltype) .length;

Return myintarray;

}

///

/// Static method, return ASCII string value according to the enumerated type

///

/// enumeration type

/// Enumeration value

/// ASCII string value

/// enumhelper.enumvaluetoasciistring (typeof (examplehexenum), ExampleHexenum.hide; obtained "("

Public Static String Enumvaluetoasciistring (System.Type Protocoltype, Object ObjectValue)

{

Return HexStringtoasciistring (enumvaluetohexString (protocoltype, objectValue);

///

/// Enter the 16-based string, return to the string that is translated into ASCII

///

///

///

/// enumhelper.hexstringtoasciistring ("

2A

"); Get the value of" * ", pay attention to the 16-en-active front mark symbol" 0x "

Public static string HexStringToasciistring (String HexString)

{

Int myint16 = int.parse (HexString, System.globalization.NumBerstyles.Allowhexspecifier);

Char mychar = (char) myint16;

Return mychar.tostring ();

}

///

/// Static method, return 16-based string values ​​according to the enumerated type

///

///

///

///

/// enumhelper.enumvaluetoHexString (typeof (exampleHexenum), ExampleHexenum.hide; get the value "00000028

Public Static String EnumvaluetoHexString (System.Type Protocoltype, Object ObjectValue)

{

Return Enum.Format (Protocoltype,

Enum.Parse (Protocoltype,

Enum.getName (Protocoltype, ObjectValue), "X");

}

///

/// Transfer the ASCII string into an enum value

///

///

///

///

/// enumhelper.asciistringToEnumValue (TypeOf (ExampleHexenum), "() Get value" examplehexenum.hide "

Public Static Object AsciistringToEnumValue (System.Type Protocoltype, String Asciistring)

{

Return HexStringToEnumValue (protocoltype, asciistringtohexstring (asciistring);}

///

/// Enter the string of ASCII, translate into a 16-en-entered string

///

///

///

/// enumhelper.asciistringtohexstring ("("); get the value "28"

Public static string asciistringtohexstring (String Normalstring) (STRING NORMALSTRING)

{

System.text.encoding enc = system.text.Encoding.Getencoding ("ASCII");

For (int i = 0; i

{

BYTE [] BS = enc.getbytes (Normalstring [i] .tostring ());

For (int J = 0; j

{

Return BS [J] .tostring ("x2");

}

}

Return "FF";

}

///

/// Translate 16 into the value of ENUM

///

///

///

///

/// enumhelper.HexStringToEnumValue (TypeOf (EXAMPLEHEXENUM), "28"); get the value "exampleHexenum.hide"

Public Static Object HexStringToEnumValue (System.Type Protocoltype (String HexString)

{

Object myObject = enum.parse (Protocoltype,

Enum.getname (Protocoltype,

INT16.PARSE (HexString, System.globalization.NumberStyles.AllowhexSpecifier));

Return myObject;

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-33580.html

New Post(0)