1、由Int值可以得到Enum对象值
2、由String值可以得到Enum对象值
3、由Hex值可以得到Enum对象值
4、Hex、ASCII值<-得到->Enum对象值
至于其应用,首先是方便写程序的时候对其进行操作;其次是用在自定义协议上,简化协议字符,但是写程序时却可以清晰的知道每个ASCII代表的Enum是什么意思;可以用在UDP传输协议上,压缩传输字符串。
1using System;
2
3namespace HxH.Collections
4{
5 public enum ExampleNormalEnum
6 {
7 Online = 1, // 1 in DB
8 Offline = 2, // 2 in DB
9 Hide = 3, // 3 in DB
10 Busy = 4, // 4 in DB
11 Away = 5, // 5 in DB
12 Other = 6, // 6 in DB
13 }
14
15 public enum ExampleHexEnum
16 {
17 Login = 0x22, //登陆服务器 ASCII = "
18 LogOff = 0x23, //退出服务器 ASCII = #
19 Online = 0x24, //在线 ASCII = $
20 Offline = 0x25, //下线 ASCII = %
21 Away = 0x26, //离开 ASCII = &
22 Busy = 0x27, //忙 ASCII = '
23 Hide = 0x28, //隐身 ASCII = (
24 }
25
26 /**//// <summary>
27 /// EnumHelper 的摘要说明。
28 /// </summary>
29 public class EnumHelper
30 {
31 public EnumHelper()
32 {
33 }
34
35 /**//// <summary>
36 /// 从Enum中任意取一个Int值,将其转化成枚举类型值
37 /// </summary>
38 /// <param name="protocolType"></param>
39 /// <param name="enumValue"></param>
40 /// <returns></returns>
41 /// <example>ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.IntValueToEnum( typeof( ExampleNormalEnum ),1); 得到值为 ExampleNormalEnum.Online </example>
42 public static object IntValueToEnum( System.Type protocolType,int enumIntValue)
43 {
44 object myObject = Enum.Parse( protocolType,Enum.GetName( protocolType, enumIntValue ));
45 return myObject;
46 }
47
48 /**//// <summary>
49 /// 从Enum中任意取一个String值,将其转化成枚举类型值
50 /// </summary>
51 /// <param name="protocolType"></param>
52 /// <param name="enumStringValue"></param>
53 /// <returns></returns>
54 /// <example>ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.StringValueToEnum( typeof( ExampleNormalEnum ),"Offline");得到值为 ExampleNormalEnum.Offline</example>
55 public static object StringValueToEnum( System.Type protocolType,string enumStringValue)
56 {
57 object myObject = Enum.Parse( protocolType,enumStringValue,true);
58 return myObject;
59 }
60
61 /**//// <summary>
62 /// 得到一个Enum中的所有Int值
63 /// </summary>
64 /// <param name="protocolType"></param>
65 /// <returns></returns>
66 public static int[] GetEnumIntValues( System.Type protocolType )
67 {
68 int[] myIntArray = new int[ Enum.GetValues( protocolType ).Length ];
69 Array.Copy( Enum.GetValues( protocolType ),myIntArray,Enum.GetValues( protocolType ).Length );
70 return myIntArray;
71 }
72
73 /**//// <summary>
74 /// 静态方法,根据枚举类型返回ASCII的字符串值
75 /// </summary>
76 /// <param name="protocolType">枚举类型</param>
77 /// <param name="objectValue">枚举值</param>
78 /// <returns>ASCII字符串值</returns>
79 /// <example>EnumHelper.EnumValueToASCIIString( typeof( ExampleHexEnum ),ExampleHexEnum.Hide );得到的值为"("</example>
80 public static string EnumValueToASCIIString( System.Type protocolType ,object objectValue)
81 {
82 return HexStringToASCIIString( EnumValueToHexString( protocolType,objectValue ) );
83 }
84
85 /**//// <summary>
86 /// 输入16进制的字符串,返回翻译成ASCII的字符串
87 /// </summary>
88 /// <param name="hexString"></param>
89 /// <returns></returns>
90 /// <example>EnumHelper.HexStringToASCIIString( "2A" ); 得到值为"*",注意去掉16进制前置标志符号"0x"</example>
91 public static string HexStringToASCIIString(string hexString)
92 {
93 int myInt16 = int.Parse( hexString,System.Globalization.NumberStyles.AllowHexSpecifier);
94 char myChar = (char)myInt16;
95 return myChar.ToString();
96 }
97
98 /**//// <summary>
99 /// 静态方法,根据枚举类型返回16进制的字符串值
100 /// </summary>
101 /// <param name="protocolType"></param>
102 /// <param name="objectValue"></param>
103 /// <returns></returns>
104 /// <example>EnumHelper.EnumValueToHexString(typeof( ExampleHexEnum ),ExampleHexEnum.Hide);得到值"00000028"</example>
105 public static string EnumValueToHexString( System.Type protocolType,object objectValue)
106 {
107 return Enum.Format( protocolType,
108 Enum.Parse( protocolType,
109 Enum.GetName( protocolType,objectValue ) ),"X" );
110 }
111
112
113 /**//// <summary>
114 /// 将ASCII字符串转换成 Enum 的值
115 /// </summary>
116 /// <param name="protocolType"></param>
117 /// <param name="asciiString"></param>
118 /// <returns></returns>
119 /// <example> EnumHelper.ASCIIStringToEnumValue( typeof( ExampleHexEnum ),"(") 得到值 "ExampleHexEnum.Hide" </example>
120 public static object ASCIIStringToEnumValue( System.Type protocolType,string asciiString)
121 {
122 return HexStringToEnumValue( protocolType, ASCIIStringToHexString( asciiString ));
123 }
124
125 /**//// <summary>
126 /// 输入ASCII的字符串,翻译成16进制的字符串
127 /// </summary>
128 /// <param name="normalString"></param>
129 /// <returns></returns>
130 /// <example>EnumHelper.ASCIIStringToHexString( "(" ); 得到值"28"</example>
131 public static string ASCIIStringToHexString(string normalString)
132 {
133 System.Text.Encoding enc = System.Text.Encoding.GetEncoding("ASCII");
134
135 for( int i=0;i<normalString.Length;++i)
136 {
137 byte[] bs = enc.GetBytes( normalString[i].ToString() );
138 for( int j=0;j<bs.Length;j++)
139 {
140 return bs[j].ToString("X2");
141 }
142 }
143
144 return "FF";
145 }
146
147 /**//// <summary>
148 /// 将16进制转换为 Enum 的值
149 /// </summary>
150 /// <param name="protocolType"></param>
151 /// <param name="hexString"></param>
152 /// <returns></returns>
153 /// <example>EnumHelper.HexStringToEnumValue( typeof( ExampleHexEnum ),"28");得到值 "ExampleHexEnum.Hide"</example>
154 public static object HexStringToEnumValue( System.Type protocolType,string hexString )
155 {
156 object myObject = Enum.Parse( protocolType,
157 Enum.GetName( protocolType ,
158 Int16.Parse( hexString ,System.Globalization.NumberStyles.AllowHexSpecifier) ) );
159
160 return myObject;
161 }
162 }
163}
本日志由 bitman 于 2005-10-17 12:07 AM 编辑]
引用通告地址: http://www.bitman.cn/trackback.asp?tbID=18