hxh(贺星河)的专栏

看美人颜红,心窃喜;纵有来生复世,知己难求;且饮酒西楼,把江山灌醉,莫问心忧!

用户操作
[即时聊天] [发私信] [加为好友]
贺星河ID:hxhbluestar
97482次访问,排名927好友0人,关注者4
hxhbluestar的文章
原创 28 篇
翻译 7 篇
转载 5 篇
评论 73 篇
贺星河的公告

>>贺星河的新浪博客<<

>>贺星河的博客园博客<<

最近评论
sap99:www.sap99.com/,SAP99资料多多

SAP免费资料下载
http://www.sap99.com

有很多的学习资料,推荐一下,
zhihui708:如何新建那个WeatherDataSet.XSD文件,是不是添加一个“XML 架构”啊
dxshenhua:我已经实现了TCP-P2P直连,包括自动识别两个用户中有一个是公网用户,两个用户在同一个局域网,两个不同内网用户的穿透直连。
caoyuan85:学习。。。。。。。
ys19011:hxh:
没有看清表述,现在理解了。全Cone、一Cone一symmetric、全在NAT后、部分NAT在、都不在NAT后确实走的是一样的流程。
文章分类
收藏
相册
SmartPhone
WebService
文章图片
我的照片
.Net
C# - OpenGL
C# Friends
C# Helper
C# Home
Codeproject
Dotnet2themax 框架库
Gotdotnet
OpenNETCF.org - Source
VS.NET2003
Windows Forms
Java
Calvin Austin J2SE5.0(RSS)
Java.net
JavaWorld
Java官方
JXTA P2P
VC++
JIURL's Doc
XML
中国XML论坛
安全专题
小榕软件实验室
网络安全焦点
帮助文档
IBM中文开发者网站
MSDN中文网
MSDN在线文档
MS搜索知识库
W3C中文翻译
操作系统
Longhorn
高级战友
AOL(RSS)
XML MVP
孟子E章
思归呓语
解决方案
Duwamish解决方案
权威站点
ACE
Linux
Python
RFC
SourceForge
UML
W3C
Web Services(RSS)
XML
设计模式
Design Patterns
Patterns in Java
项目管理
CVS
WorldHello
友情链接
CoolBug
Johnson
林星
海华
热闹城市-市长日记
祖尔谈软件
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

原创 随心所欲操作Enum枚举类型收藏

新一篇: 是偶然,也是必然 | 旧一篇: Peer-to-Peer (P2P) communication across middleboxes(翻译7)

简单的写了一个操作Enum枚举类型的Helper,实现如下功能:
1、由Int值可以得到Enum对象值
2、由String值可以得到Enum对象值
3、由Hex值可以得到Enum对象值
4、Hex、ASCII值<-得到->Enum对象值


至于其应用,首先是方便写程序的时候对其进行操作;其次是用在自定义协议上,简化协议字符,但是写程序时却可以清晰的知道每个ASCII代表的Enum是什么意思;可以用在UDP传输协议上,压缩传输字符串。

using System;

 

namespace HxH.Collections

{

     public enum ExampleNormalEnum

     {

         Online   = 1,          //   1    in DB

         Offline  = 2,          //   2    in DB

         Hide = 3,          //   3    in DB

         Busy = 4,          //   4    in DB

         Away = 5,          //   5    in DB

         Other    = 6,          //   6    in DB

     }

 

     public enum ExampleHexEnum

     {

         Login                  = 0x22,       //登陆服务器  ASCII = "

         LogOff                 = 0x23,       //退出服务器  ASCII = #

         Online                 = 0x24,       //在线             ASCII = $

         Offline                = 0x25,       //下线             ASCII = %

         Away                   = 0x26,       //离开             ASCII = &

         Busy                   = 0x27,       //          ASCII = '

         Hide                   = 0x28,       //隐身             ASCII = (

     }

 

     /// <summary>

     /// EnumHelper 的摘要说明。

     /// </summary>

     public class EnumHelper

     {

         public EnumHelper()

         {

         }

 

         /// <summary>

         /// Enum中任意取一个Int值,将其转化成枚举类型值

         /// </summary>

         /// <param name="protocolType"></param>

         /// <param name="enumValue"></param>

         /// <returns></returns>

         /// <example>ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.IntValueToEnum( typeof( ExampleNormalEnum ),1); 得到值为 ExampleNormalEnum.Online </example>

         public static object IntValueToEnum( System.Type protocolType,int enumIntValue)

         {

              object myObject = Enum.Parse( protocolType,Enum.GetName( protocolType, enumIntValue ));

              return myObject;

         }

 

         /// <summary>

         /// Enum中任意取一个String值,将其转化成枚举类型值

         /// </summary>

         /// <param name="protocolType"></param>

         /// <param name="enumStringValue"></param>

         /// <returns></returns>

         /// <example>ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.StringValueToEnum( typeof( ExampleNormalEnum ),"Offline");得到值为 ExampleNormalEnum.Offline</example>

         public static object StringValueToEnum( System.Type protocolType,string enumStringValue)

         {

              object myObject = Enum.Parse( protocolType,enumStringValue,true);

              return myObject;

         }

 

         /// <summary>

         /// 得到一个Enum中的所有Int

         /// </summary>

         /// <param name="protocolType"></param>

         /// <returns></returns>

         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;

         }

 

         /// <summary>

         /// 静态方法,根据枚举类型返回ASCII的字符串值

         /// </summary>

         /// <param name="protocolType">枚举类型</param>

         /// <param name="objectValue">枚举值</param>

         /// <returns>ASCII字符串值</returns>

         /// <example>EnumHelper.EnumValueToASCIIString( typeof( ExampleHexEnum ),ExampleHexEnum.Hide );得到的值为"("</example>

         public static string EnumValueToASCIIString( System.Type protocolType ,object objectValue)

         {

              return HexStringToASCIIString( EnumValueToHexString( protocolType,objectValue ) );

         }

 

         /// <summary>

         /// 输入16进制的字符串,返回翻译成ASCII的字符串

         /// </summary>

         /// <param name="hexString"></param>

         /// <returns></returns>

         /// <example>EnumHelper.HexStringToASCIIString( "2A" ); 得到值为"*",注意去掉16进制前置标志符号"0x"</example>

         public static string HexStringToASCIIString(string hexString)

         {

              int myInt16 = int.Parse( hexString,System.Globalization.NumberStyles.AllowHexSpecifier);

              char myChar = (char)myInt16;

              return myChar.ToString();

         }

 

         /// <summary>

         /// 静态方法,根据枚举类型返回16进制的字符串值

         /// </summary>

         /// <param name="protocolType"></param>

         /// <param name="objectValue"></param>

         /// <returns></returns>

         /// <example>EnumHelper.EnumValueToHexString(typeof( ExampleHexEnum ),ExampleHexEnum.Hide);得到值"00000028"</example>

         public static string EnumValueToHexString( System.Type protocolType,object objectValue)

         {

              return Enum.Format( protocolType,

                   Enum.Parse( protocolType,

                   Enum.GetName( protocolType,objectValue ) ),"X" );

         }

 

 

         /// <summary>

         /// ASCII字符串转换成 Enum 的值

         /// </summary>

         /// <param name="protocolType"></param>

         /// <param name="asciiString"></param>

         /// <returns></returns>

         /// <example> EnumHelper.ASCIIStringToEnumValue( typeof( ExampleHexEnum ),"(") 得到值 "ExampleHexEnum.Hide" </example>

         public static object ASCIIStringToEnumValue( System.Type protocolType,string asciiString)

         {

              return HexStringToEnumValue( protocolType, ASCIIStringToHexString( asciiString ));

         }

 

         /// <summary>

         /// 输入ASCII的字符串,翻译成16进制的字符串

         /// </summary>

         /// <param name="normalString"></param>

         /// <returns></returns>

         /// <example>EnumHelper.ASCIIStringToHexString( "(" ); 得到值"28"</example>

         public static string ASCIIStringToHexString(string normalString)

         {

              System.Text.Encoding enc = System.Text.Encoding.GetEncoding("ASCII");

 

              for( int i=0;i<normalString.Length;++i)

              {

                   byte[] bs = enc.GetBytes( normalString[i].ToString() );

                   for( int j=0;j<bs.Length;j++)

                   {

                       return bs[j].ToString("X2");

                   }

              }

 

              return "FF";

         }

        

         /// <summary>

         /// 16进制转换为 Enum 的值

         /// </summary>

         /// <param name="protocolType"></param>

         /// <param name="hexString"></param>

         /// <returns></returns>

         /// <example>EnumHelper.HexStringToEnumValue( typeof( ExampleHexEnum ),"28");得到值 "ExampleHexEnum.Hide"</example>

         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;

         }

     }

}

发表于 @ 2005年04月21日 09:59:00|评论(loading...)|编辑

新一篇: 是偶然,也是必然 | 旧一篇: Peer-to-Peer (P2P) communication across middleboxes(翻译7)

评论

#caoyuan85 发表于2007-06-07 16:07:41  IP: 220.231.1.*
学习。。。。。。。
发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 贺星河