[netMarketing类库] ConvertHelper类: 处理数据类型转换,数制转换、编码转换相关的类

命名空间:netMarketing.ConvertAbout.ConvertHelper

功能:处理数据类型转换,数制转换、编码转换相关的类


功能详细说明:

这个类封装了处理数据类型转换,数制转换、编码转换相关的一些常用处理方法。

image.png

常用函数列表

ConvertHelper.rs232Convert类的方法
==================
/// 字符串转16进制串,用空格分隔
public static string StringToHexString(string s, Encoding encode)
/// 十六进制串转字符串
public static string HexStringToString(string hs, Encoding encode)
/// 字符串转16进制字节数组
public static byte[] strToToHexByte(string hexString)
/// 字节数组转16进制字符串
public static string byteToHexStr(byte[] bytes)
/// 从汉字转换到16进制
public static string ToHex(string s, string charset, bool fenge)
/// 从16进制转换成汉字
public static string UnHex(string hex, string charset)



ConvertHelper类的方法
==================
#region 汉字转拼音
/// 取单字拼音
 public static string pinYingGet(char hz)
/// 取单字拼音第一个字段
 public static string pinYingFirst(char hz)

#region 各进制数间转换
/// 实现各进制数间的转换。ConvertBase("15",10,16)表示将十进制数15转换为16进制的数。
public static string ConvertBase<T>(T value, int from, int to)
/// 判定Type是不是简单数据型
public static bool isSimpleDataType(this Type dataType)
/// 取变量的sizeof()大小,如果返回0,则传入变量不属于简单数据类型
public static int getVarSizeOf<T>(T obj)

#region 使用指定字符集将string转换成byte[]
/// 将string转换成byte[]
public static byte[] StringToBytes(string text)
/// 使用指定字符集将string转换成byte[]
public static byte[] StringToBytes(string text, Encoding encoding)

#region 使用指定字符集将byte[]转换成string
/// 将byte[]转换成string
public static string BytesToString(byte[] bytes)
/// 使用指定字符集将byte[]转换成string
public static string BytesToString(byte[] bytes, Encoding encoding)


 #region 将byte[]转换成int
/// 将byte[]转换成int
public static int BytesToInt32(byte[] data)

 #region 将数据转换为整型
/// 将数据转换为整型   转换失败返回默认值
public static int ToInt32<T>(T data, int defValue)
/// 将数据转换为整型   转换失败返回默认值
public static int ToInt32(string data, int defValue)
/// 将数据转换为整型  转换失败返回默认值
public static int ToInt32(object data, int defValue)

#region 将数据转换为布尔型
/// 将数据转换为布尔类型  转换失败返回默认值
public static bool ToBoolean<T>(T data, bool defValue)
/// 将数据转换为布尔类型  转换失败返回 默认值
public static bool ToBoolean(string data, bool defValue)
/// 将数据转换为布尔类型  转换失败返回 默认值
public static bool ToBoolean(object data, bool defValue)

#region 将数据转换为单精度浮点型
/// 将数据转换为单精度浮点型  转换失败 返回默认值
public static float ToFloat<T>(T data, float defValue)
/// 将数据转换为单精度浮点型   转换失败返回默认值
public static float ToFloat(object data, float defValue)
 /// 将数据转换为单精度浮点型   转换失败返回默认值
public static float ToFloat(string data, float defValue)
/// 将数据转换为short型   转换失败返回默认值
public static short ToShort(string data, short defValue)

 #region 将数据转换为双精度浮点型
/// 将数据转换为双精度浮点型   转换失败返回默认值
 public static double ToDouble<T>(T data, double defValue)
/// 将数据转换为双精度浮点型,并设置小数位   转换失败返回默认值
public static double ToDouble<T>(T data, int decimals, double defValue)
/// 将数据转换为双精度浮点型  转换失败返回默认值
public static double ToDouble(object data, double defValue)
/// 将数据转换为双精度浮点型,并设置小数位  转换失败返回默认值
 public static double ToDouble(object data, int decimals, double defValue)
/// 将数据转换为双精度浮点型,并设置小数位  转换失败返回默认值
public static double ToDouble(string data, int decimals, double defValue)


#region 将数据转换为指定类型
/// 将数据转换为指定类型
public static object ConvertTo(object data, Type targetType)
/// 将数据转换为指定类型
public static T ConvertTo<T>(object data)


#region 将数据转换Decimal
/// 将数据转换为Decimal  转换失败返回默认值
public static Decimal ToDecimal<T>(T data, Decimal defValue)
/// 将数据转换为Decimal  转换失败返回 默认值
public static Decimal ToDecimal(object data, Decimal defValue)
/// 将数据转换为Decimal  转换失败返回 默认值
 public static Decimal ToDecimal(string data, Decimal defValue)

#region 将数据转换为DateTime
/// 将数据转换为DateTime  转换失败返回默认值
public static DateTime ToDateTime<T>(T data, DateTime defValue)
/// 将数据转换为DateTime  转换失败返回 默认值
public static DateTime ToDateTime(object data, DateTime defValue)
/// 将数据转换为DateTime  转换失败返回 默认值
public static DateTime ToDateTime(string data, DateTime defValue)


 #region 半角全角转换
/// 转全角的函数(SBC case)
public static string ConvertToSBC(string input)
/// <summary> 转半角的函数(DBC case) </summary>
public static string ConvertToDBC(string input)


ConvertHelper.ToXXX类的方法
==================
 #region 类型转换
/// 取得Int值,如果为Null或者非数值, 则返回0
public static int ToInt(object obj)
/// 取得float值,如果为null或者为非数值,返回0
public static float ToFloat(object obj)
/// 取得Int值,如果不成功则返回指定exceptionvalue值
public static int ToInt(object obj, int exceptionvalue)
/// 取得byte值
 public static byte ToByte(object obj)
/// 16进制串取得byte值
public static byte HexToByte(string obj)
/// 获得Long值
public static long ToLong(object obj)
/// 取得Long值,如果不成功则返回指定exceptionvalue值
public static long ToLong(object obj, long exceptionvalue)
/// 取得Decimal值
public static decimal ToDecimal(object obj)
/// 取得DateTime值
public static DateTime ToDateTime(object obj)
/// 格式化日期 yyyy-MM-dd HH:mm
 public static string FormatDateTime(object obj, string Format)
/// 取得bool值
public static bool ToBool(object obj)

--------------------- 

作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!

返回类库功能说明目录


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864