少有人走的路

勇哥的工业自动化技术网站

[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
版权声明:本文为博主原创文章,转载请附上博文链接!

返回类库功能说明目录


发表评论:

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

«    2026年2月    »
1
2345678
9101112131415
16171819202122
232425262728
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.3

Copyright www.skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864