123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ProjectBase.Util
- {
- /// <summary>
- /// 处理数据类型转换,数制转换、编码转换相关的类
- /// </summary>
- public sealed class ConvertHelper
- {
- #region 各进制数间转换
- /// <summary>
- /// 实现各进制数间的转换。ConvertBase("15",10,16)表示将十进制数15转换为16进制的数。
- /// </summary>
- /// <param name="value">要转换的值,即原值</param>
- /// <param name="from">原值的进制,只能是2,8,10,16四个值。</param>
- /// <param name="to">要转换到的目标进制,只能是2,8,10,16四个值。</param>
- public static string ConvertBase(string value, int from, int to)
- {
- if (!isBaseNumber(from))
- throw new ArgumentException("参数from只能是2,8,10,16四个值。");
- if (!isBaseNumber(to))
- throw new ArgumentException("参数to只能是2,8,10,16四个值。");
- int intValue = Convert.ToInt32(value, from); //先转成10进制
- string result = Convert.ToString(intValue, to); //再转成目标进制
- if (to == 2)
- {
- int resultLength = result.Length; //获取二进制的长度
- switch (resultLength)
- {
- case 7:
- result = "0" + result;
- break;
- case 6:
- result = "00" + result;
- break;
- case 5:
- result = "000" + result;
- break;
- case 4:
- result = "0000" + result;
- break;
- case 3:
- result = "00000" + result;
- break;
- }
- }
- return result;
- }
- /// <summary>
- /// 判断是否是 2 8 10 16
- /// </summary>
- /// <param name="baseNumber"></param>
- /// <returns></returns>
- private static bool isBaseNumber(int baseNumber)
- {
- if (baseNumber == 2 || baseNumber == 8 || baseNumber == 10 || baseNumber == 16)
- return true;
- return false;
- }
- #endregion
- #region 使用指定字符集将string转换成byte[]
- /// <summary>
- /// 将string转换成byte[]
- /// </summary>
- /// <param name="text">要转换的字符串</param>
- public static byte[] StringToBytes(string text)
- {
- return Encoding.Default.GetBytes(text);
- }
- /// <summary>
- /// 使用指定字符集将string转换成byte[]
- /// </summary>
- /// <param name="text">要转换的字符串</param>
- /// <param name="encoding">字符编码</param>
- public static byte[] StringToBytes(string text, Encoding encoding)
- {
- return encoding.GetBytes(text);
- }
- #endregion
- #region 使用指定字符集将byte[]转换成string
- /// <summary>
- /// 将byte[]转换成string
- /// </summary>
- /// <param name="bytes">要转换的字节数组</param>
- public static string BytesToString(byte[] bytes)
- {
- return Encoding.Default.GetString(bytes);
- }
- /// <summary>
- /// 使用指定字符集将byte[]转换成string
- /// </summary>
- /// <param name="bytes">要转换的字节数组</param>
- /// <param name="encoding">字符编码</param>
- public static string BytesToString(byte[] bytes, Encoding encoding)
- {
- return encoding.GetString(bytes);
- }
- #endregion
- #region 将byte[]转换成int
- /// <summary>
- /// 将byte[]转换成int
- /// </summary>
- /// <param name="data">需要转换成整数的byte数组</param>
- public static int BytesToInt32(byte[] data)
- {
- //如果传入的字节数组长度小于4,则返回0
- if (data.Length < 4)
- {
- return 0;
- }
- //定义要返回的整数
- int num = 0;
- //如果传入的字节数组长度大于4,需要进行处理
- if (data.Length >= 4)
- {
- //创建一个临时缓冲区
- byte[] tempBuffer = new byte[4];
- //将传入的字节数组的前4个字节复制到临时缓冲区
- Buffer.BlockCopy(data, 0, tempBuffer, 0, 4);
- //将临时缓冲区的值转换成整数,并赋给num
- num = BitConverter.ToInt32(tempBuffer, 0);
- }
- //返回整数
- return num;
- }
- #endregion
- #region 将数据转换为整型
- /// <summary>
- /// 将数据转换为整型 转换失败返回默认值
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static int ToInt32<T>(T data, int defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToInt32(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为整型 转换失败返回默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static int ToInt32(string data, int defValue)
- {
- //如果为空则返回默认值
- if (string.IsNullOrEmpty(data))
- {
- return defValue;
- }
- int temp = 0;
- if (Int32.TryParse(data, out temp))
- {
- return temp;
- }
- else
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为整型 转换失败返回默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static int ToInt32(object data, int defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToInt32(data);
- }
- catch
- {
- return defValue;
- }
- }
- #endregion
- #region 将数据转换为布尔型
- /// <summary>
- /// 将数据转换为布尔类型 转换失败返回默认值
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static bool ToBoolean<T>(T data, bool defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToBoolean(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为布尔类型 转换失败返回 默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static bool ToBoolean(string data, bool defValue)
- {
- //如果为空则返回默认值
- if (string.IsNullOrEmpty(data))
- {
- return defValue;
- }
- bool temp = false;
- if (bool.TryParse(data, out temp))
- {
- return temp;
- }
- else
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为布尔类型 转换失败返回 默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static bool ToBoolean(object data, bool defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToBoolean(data);
- }
- catch
- {
- return defValue;
- }
- }
- #endregion
- #region 将数据转换为单精度浮点型
- /// <summary>
- /// 将数据转换为单精度浮点型 转换失败 返回默认值
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static float ToFloat<T>(T data, float defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToSingle(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为单精度浮点型 转换失败返回默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static float ToFloat(object data, float defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToSingle(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为单精度浮点型 转换失败返回默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static float ToFloat(string data, float defValue)
- {
- //如果为空则返回默认值
- if (string.IsNullOrEmpty(data))
- {
- return defValue;
- }
- float temp = 0;
- if (float.TryParse(data, out temp))
- {
- return temp;
- }
- else
- {
- return defValue;
- }
- }
- #endregion
- #region 将数据转换为双精度浮点型
- /// <summary>
- /// 将数据转换为双精度浮点型 转换失败返回默认值
- /// </summary>
- /// <typeparam name="T">数据的类型</typeparam>
- /// <param name="data">要转换的数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static double ToDouble<T>(T data, double defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToDouble(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为双精度浮点型,并设置小数位 转换失败返回默认值
- /// </summary>
- /// <typeparam name="T">数据的类型</typeparam>
- /// <param name="data">要转换的数据</param>
- /// <param name="decimals">小数的位数</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static double ToDouble<T>(T data, int decimals, double defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Math.Round(Convert.ToDouble(data), decimals);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为双精度浮点型 转换失败返回默认值
- /// </summary>
- /// <param name="data">要转换的数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static double ToDouble(object data, double defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToDouble(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为双精度浮点型 转换失败返回默认值
- /// </summary>
- /// <param name="data">要转换的数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static double ToDouble(string data, double defValue)
- {
- //如果为空则返回默认值
- if (string.IsNullOrEmpty(data))
- {
- return defValue;
- }
- double temp = 0;
- if (double.TryParse(data, out temp))
- {
- return temp;
- }
- else
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为双精度浮点型,并设置小数位 转换失败返回默认值
- /// </summary>
- /// <param name="data">要转换的数据</param>
- /// <param name="decimals">小数的位数</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static double ToDouble(object data, int decimals, double defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Math.Round(Convert.ToDouble(data), decimals);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为双精度浮点型,并设置小数位 转换失败返回默认值
- /// </summary>
- /// <param name="data">要转换的数据</param>
- /// <param name="decimals">小数的位数</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static double ToDouble(string data, int decimals, double defValue)
- {
- //如果为空则返回默认值
- if (string.IsNullOrEmpty(data))
- {
- return defValue;
- }
- double temp = 0;
- if (double.TryParse(data, out temp))
- {
- return Math.Round(temp, decimals);
- }
- else
- {
- return defValue;
- }
- }
- #endregion
- #region 将数据转换为指定类型
- /// <summary>
- /// 将数据转换为指定类型
- /// </summary>
- /// <param name="data">转换的数据</param>
- /// <param name="targetType">转换的目标类型</param>
- public static object ConvertTo(object data, Type targetType)
- {
- if (data == null || Convert.IsDBNull(data))
- {
- return null;
- }
- Type type2 = data.GetType();
- if (targetType == type2)
- {
- return data;
- }
- if (((targetType == typeof(Guid)) || (targetType == typeof(Guid?))) && (type2 == typeof(string)))
- {
- if (string.IsNullOrEmpty(data.ToString()))
- {
- return null;
- }
- return new Guid(data.ToString());
- }
- if (targetType.IsEnum)
- {
- try
- {
- return Enum.Parse(targetType, data.ToString(), true);
- }
- catch
- {
- return Enum.ToObject(targetType, data);
- }
- }
- if (targetType.IsGenericType)
- {
- targetType = targetType.GetGenericArguments()[0];
- }
- return Convert.ChangeType(data, targetType);
- }
- /// <summary>
- /// 将数据转换为指定类型
- /// </summary>
- /// <typeparam name="T">转换的目标类型</typeparam>
- /// <param name="data">转换的数据</param>
- public static T ConvertTo<T>(object data)
- {
- if (data == null || Convert.IsDBNull(data))
- return default(T);
- object obj = ConvertTo(data, typeof(T));
- if (obj == null)
- {
- return default(T);
- }
- return (T)obj;
- }
- #endregion
- #region 将数据转换Decimal
- /// <summary>
- /// 将数据转换为Decimal 转换失败返回默认值
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static Decimal ToDecimal<T>(T data, Decimal defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToDecimal(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为Decimal 转换失败返回 默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static Decimal ToDecimal(object data, Decimal defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToDecimal(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为Decimal 转换失败返回 默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static Decimal ToDecimal(string data, Decimal defValue)
- {
- //如果为空则返回默认值
- if (string.IsNullOrEmpty(data))
- {
- return defValue;
- }
- decimal temp = 0;
- if (decimal.TryParse(data, out temp))
- {
- return temp;
- }
- else
- {
- return defValue;
- }
- }
- #endregion
- #region 将数据转换为DateTime
- /// <summary>
- /// 将数据转换为DateTime 转换失败返回默认值
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static DateTime ToDateTime<T>(T data, DateTime defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToDateTime(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为DateTime 转换失败返回 默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static DateTime ToDateTime(object data, DateTime defValue)
- {
- //如果为空则返回默认值
- if (data == null || Convert.IsDBNull(data))
- {
- return defValue;
- }
- try
- {
- return Convert.ToDateTime(data);
- }
- catch
- {
- return defValue;
- }
- }
- /// <summary>
- /// 将数据转换为DateTime 转换失败返回 默认值
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="defValue">默认值</param>
- /// <returns></returns>
- public static DateTime ToDateTime(string data, DateTime defValue)
- {
- //如果为空则返回默认值
- if (string.IsNullOrEmpty(data))
- {
- return defValue;
- }
- DateTime temp = DateTime.Now;
- if (DateTime.TryParse(data, out temp))
- {
- return temp;
- }
- else
- {
- return defValue;
- }
- }
- #endregion
- #region 半角全角转换
- /// <summary>
- /// 转全角的函数(SBC case)
- /// </summary>
- /// <param name="input">任意字符串</param>
- /// <returns>全角字符串</returns>
- ///<remarks>
- ///全角空格为12288,半角空格为32
- ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- ///</remarks>
- public static string ConvertToSBC(string input)
- {
- //半角转全角:
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 32)
- {
- c[i] = (char)12288;
- continue;
- }
- if (c[i] < 127)
- {
- c[i] = (char)(c[i] + 65248);
- }
- }
- return new string(c);
- }
- /// <summary> 转半角的函数(DBC case) </summary>
- /// <param name="input">任意字符串</param>
- /// <returns>半角字符串</returns>
- ///<remarks>
- ///全角空格为12288,半角空格为32
- ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- ///</remarks>
- public static string ConvertToDBC(string input)
- {
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- c[i] = (char)(c[i] - 65248);
- }
- return new string(c);
- }
- #endregion
- }
- }
|