ConvertHelper.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ProjectBase.Util
  7. {
  8. /// <summary>
  9. /// 处理数据类型转换,数制转换、编码转换相关的类
  10. /// </summary>
  11. public sealed class ConvertHelper
  12. {
  13. #region 各进制数间转换
  14. /// <summary>
  15. /// 实现各进制数间的转换。ConvertBase("15",10,16)表示将十进制数15转换为16进制的数。
  16. /// </summary>
  17. /// <param name="value">要转换的值,即原值</param>
  18. /// <param name="from">原值的进制,只能是2,8,10,16四个值。</param>
  19. /// <param name="to">要转换到的目标进制,只能是2,8,10,16四个值。</param>
  20. public static string ConvertBase(string value, int from, int to)
  21. {
  22. if (!isBaseNumber(from))
  23. throw new ArgumentException("参数from只能是2,8,10,16四个值。");
  24. if (!isBaseNumber(to))
  25. throw new ArgumentException("参数to只能是2,8,10,16四个值。");
  26. int intValue = Convert.ToInt32(value, from); //先转成10进制
  27. string result = Convert.ToString(intValue, to); //再转成目标进制
  28. if (to == 2)
  29. {
  30. int resultLength = result.Length; //获取二进制的长度
  31. switch (resultLength)
  32. {
  33. case 7:
  34. result = "0" + result;
  35. break;
  36. case 6:
  37. result = "00" + result;
  38. break;
  39. case 5:
  40. result = "000" + result;
  41. break;
  42. case 4:
  43. result = "0000" + result;
  44. break;
  45. case 3:
  46. result = "00000" + result;
  47. break;
  48. }
  49. }
  50. return result;
  51. }
  52. /// <summary>
  53. /// 判断是否是 2 8 10 16
  54. /// </summary>
  55. /// <param name="baseNumber"></param>
  56. /// <returns></returns>
  57. private static bool isBaseNumber(int baseNumber)
  58. {
  59. if (baseNumber == 2 || baseNumber == 8 || baseNumber == 10 || baseNumber == 16)
  60. return true;
  61. return false;
  62. }
  63. #endregion
  64. #region 使用指定字符集将string转换成byte[]
  65. /// <summary>
  66. /// 将string转换成byte[]
  67. /// </summary>
  68. /// <param name="text">要转换的字符串</param>
  69. public static byte[] StringToBytes(string text)
  70. {
  71. return Encoding.Default.GetBytes(text);
  72. }
  73. /// <summary>
  74. /// 使用指定字符集将string转换成byte[]
  75. /// </summary>
  76. /// <param name="text">要转换的字符串</param>
  77. /// <param name="encoding">字符编码</param>
  78. public static byte[] StringToBytes(string text, Encoding encoding)
  79. {
  80. return encoding.GetBytes(text);
  81. }
  82. #endregion
  83. #region 使用指定字符集将byte[]转换成string
  84. /// <summary>
  85. /// 将byte[]转换成string
  86. /// </summary>
  87. /// <param name="bytes">要转换的字节数组</param>
  88. public static string BytesToString(byte[] bytes)
  89. {
  90. return Encoding.Default.GetString(bytes);
  91. }
  92. /// <summary>
  93. /// 使用指定字符集将byte[]转换成string
  94. /// </summary>
  95. /// <param name="bytes">要转换的字节数组</param>
  96. /// <param name="encoding">字符编码</param>
  97. public static string BytesToString(byte[] bytes, Encoding encoding)
  98. {
  99. return encoding.GetString(bytes);
  100. }
  101. #endregion
  102. #region 将byte[]转换成int
  103. /// <summary>
  104. /// 将byte[]转换成int
  105. /// </summary>
  106. /// <param name="data">需要转换成整数的byte数组</param>
  107. public static int BytesToInt32(byte[] data)
  108. {
  109. //如果传入的字节数组长度小于4,则返回0
  110. if (data.Length < 4)
  111. {
  112. return 0;
  113. }
  114. //定义要返回的整数
  115. int num = 0;
  116. //如果传入的字节数组长度大于4,需要进行处理
  117. if (data.Length >= 4)
  118. {
  119. //创建一个临时缓冲区
  120. byte[] tempBuffer = new byte[4];
  121. //将传入的字节数组的前4个字节复制到临时缓冲区
  122. Buffer.BlockCopy(data, 0, tempBuffer, 0, 4);
  123. //将临时缓冲区的值转换成整数,并赋给num
  124. num = BitConverter.ToInt32(tempBuffer, 0);
  125. }
  126. //返回整数
  127. return num;
  128. }
  129. #endregion
  130. #region 将数据转换为整型
  131. /// <summary>
  132. /// 将数据转换为整型 转换失败返回默认值
  133. /// </summary>
  134. /// <typeparam name="T">数据类型</typeparam>
  135. /// <param name="data">数据</param>
  136. /// <param name="defValue">默认值</param>
  137. /// <returns></returns>
  138. public static int ToInt32<T>(T data, int defValue)
  139. {
  140. //如果为空则返回默认值
  141. if (data == null || Convert.IsDBNull(data))
  142. {
  143. return defValue;
  144. }
  145. try
  146. {
  147. return Convert.ToInt32(data);
  148. }
  149. catch
  150. {
  151. return defValue;
  152. }
  153. }
  154. /// <summary>
  155. /// 将数据转换为整型 转换失败返回默认值
  156. /// </summary>
  157. /// <param name="data">数据</param>
  158. /// <param name="defValue">默认值</param>
  159. /// <returns></returns>
  160. public static int ToInt32(string data, int defValue)
  161. {
  162. //如果为空则返回默认值
  163. if (string.IsNullOrEmpty(data))
  164. {
  165. return defValue;
  166. }
  167. int temp = 0;
  168. if (Int32.TryParse(data, out temp))
  169. {
  170. return temp;
  171. }
  172. else
  173. {
  174. return defValue;
  175. }
  176. }
  177. /// <summary>
  178. /// 将数据转换为整型 转换失败返回默认值
  179. /// </summary>
  180. /// <param name="data">数据</param>
  181. /// <param name="defValue">默认值</param>
  182. /// <returns></returns>
  183. public static int ToInt32(object data, int defValue)
  184. {
  185. //如果为空则返回默认值
  186. if (data == null || Convert.IsDBNull(data))
  187. {
  188. return defValue;
  189. }
  190. try
  191. {
  192. return Convert.ToInt32(data);
  193. }
  194. catch
  195. {
  196. return defValue;
  197. }
  198. }
  199. #endregion
  200. #region 将数据转换为布尔型
  201. /// <summary>
  202. /// 将数据转换为布尔类型 转换失败返回默认值
  203. /// </summary>
  204. /// <typeparam name="T">数据类型</typeparam>
  205. /// <param name="data">数据</param>
  206. /// <param name="defValue">默认值</param>
  207. /// <returns></returns>
  208. public static bool ToBoolean<T>(T data, bool defValue)
  209. {
  210. //如果为空则返回默认值
  211. if (data == null || Convert.IsDBNull(data))
  212. {
  213. return defValue;
  214. }
  215. try
  216. {
  217. return Convert.ToBoolean(data);
  218. }
  219. catch
  220. {
  221. return defValue;
  222. }
  223. }
  224. /// <summary>
  225. /// 将数据转换为布尔类型 转换失败返回 默认值
  226. /// </summary>
  227. /// <param name="data">数据</param>
  228. /// <param name="defValue">默认值</param>
  229. /// <returns></returns>
  230. public static bool ToBoolean(string data, bool defValue)
  231. {
  232. //如果为空则返回默认值
  233. if (string.IsNullOrEmpty(data))
  234. {
  235. return defValue;
  236. }
  237. bool temp = false;
  238. if (bool.TryParse(data, out temp))
  239. {
  240. return temp;
  241. }
  242. else
  243. {
  244. return defValue;
  245. }
  246. }
  247. /// <summary>
  248. /// 将数据转换为布尔类型 转换失败返回 默认值
  249. /// </summary>
  250. /// <param name="data">数据</param>
  251. /// <param name="defValue">默认值</param>
  252. /// <returns></returns>
  253. public static bool ToBoolean(object data, bool defValue)
  254. {
  255. //如果为空则返回默认值
  256. if (data == null || Convert.IsDBNull(data))
  257. {
  258. return defValue;
  259. }
  260. try
  261. {
  262. return Convert.ToBoolean(data);
  263. }
  264. catch
  265. {
  266. return defValue;
  267. }
  268. }
  269. #endregion
  270. #region 将数据转换为单精度浮点型
  271. /// <summary>
  272. /// 将数据转换为单精度浮点型 转换失败 返回默认值
  273. /// </summary>
  274. /// <typeparam name="T">数据类型</typeparam>
  275. /// <param name="data">数据</param>
  276. /// <param name="defValue">默认值</param>
  277. /// <returns></returns>
  278. public static float ToFloat<T>(T data, float defValue)
  279. {
  280. //如果为空则返回默认值
  281. if (data == null || Convert.IsDBNull(data))
  282. {
  283. return defValue;
  284. }
  285. try
  286. {
  287. return Convert.ToSingle(data);
  288. }
  289. catch
  290. {
  291. return defValue;
  292. }
  293. }
  294. /// <summary>
  295. /// 将数据转换为单精度浮点型 转换失败返回默认值
  296. /// </summary>
  297. /// <param name="data">数据</param>
  298. /// <param name="defValue">默认值</param>
  299. /// <returns></returns>
  300. public static float ToFloat(object data, float defValue)
  301. {
  302. //如果为空则返回默认值
  303. if (data == null || Convert.IsDBNull(data))
  304. {
  305. return defValue;
  306. }
  307. try
  308. {
  309. return Convert.ToSingle(data);
  310. }
  311. catch
  312. {
  313. return defValue;
  314. }
  315. }
  316. /// <summary>
  317. /// 将数据转换为单精度浮点型 转换失败返回默认值
  318. /// </summary>
  319. /// <param name="data">数据</param>
  320. /// <param name="defValue">默认值</param>
  321. /// <returns></returns>
  322. public static float ToFloat(string data, float defValue)
  323. {
  324. //如果为空则返回默认值
  325. if (string.IsNullOrEmpty(data))
  326. {
  327. return defValue;
  328. }
  329. float temp = 0;
  330. if (float.TryParse(data, out temp))
  331. {
  332. return temp;
  333. }
  334. else
  335. {
  336. return defValue;
  337. }
  338. }
  339. #endregion
  340. #region 将数据转换为双精度浮点型
  341. /// <summary>
  342. /// 将数据转换为双精度浮点型 转换失败返回默认值
  343. /// </summary>
  344. /// <typeparam name="T">数据的类型</typeparam>
  345. /// <param name="data">要转换的数据</param>
  346. /// <param name="defValue">默认值</param>
  347. /// <returns></returns>
  348. public static double ToDouble<T>(T data, double defValue)
  349. {
  350. //如果为空则返回默认值
  351. if (data == null || Convert.IsDBNull(data))
  352. {
  353. return defValue;
  354. }
  355. try
  356. {
  357. return Convert.ToDouble(data);
  358. }
  359. catch
  360. {
  361. return defValue;
  362. }
  363. }
  364. /// <summary>
  365. /// 将数据转换为双精度浮点型,并设置小数位 转换失败返回默认值
  366. /// </summary>
  367. /// <typeparam name="T">数据的类型</typeparam>
  368. /// <param name="data">要转换的数据</param>
  369. /// <param name="decimals">小数的位数</param>
  370. /// <param name="defValue">默认值</param>
  371. /// <returns></returns>
  372. public static double ToDouble<T>(T data, int decimals, double defValue)
  373. {
  374. //如果为空则返回默认值
  375. if (data == null || Convert.IsDBNull(data))
  376. {
  377. return defValue;
  378. }
  379. try
  380. {
  381. return Math.Round(Convert.ToDouble(data), decimals);
  382. }
  383. catch
  384. {
  385. return defValue;
  386. }
  387. }
  388. /// <summary>
  389. /// 将数据转换为双精度浮点型 转换失败返回默认值
  390. /// </summary>
  391. /// <param name="data">要转换的数据</param>
  392. /// <param name="defValue">默认值</param>
  393. /// <returns></returns>
  394. public static double ToDouble(object data, double defValue)
  395. {
  396. //如果为空则返回默认值
  397. if (data == null || Convert.IsDBNull(data))
  398. {
  399. return defValue;
  400. }
  401. try
  402. {
  403. return Convert.ToDouble(data);
  404. }
  405. catch
  406. {
  407. return defValue;
  408. }
  409. }
  410. /// <summary>
  411. /// 将数据转换为双精度浮点型 转换失败返回默认值
  412. /// </summary>
  413. /// <param name="data">要转换的数据</param>
  414. /// <param name="defValue">默认值</param>
  415. /// <returns></returns>
  416. public static double ToDouble(string data, double defValue)
  417. {
  418. //如果为空则返回默认值
  419. if (string.IsNullOrEmpty(data))
  420. {
  421. return defValue;
  422. }
  423. double temp = 0;
  424. if (double.TryParse(data, out temp))
  425. {
  426. return temp;
  427. }
  428. else
  429. {
  430. return defValue;
  431. }
  432. }
  433. /// <summary>
  434. /// 将数据转换为双精度浮点型,并设置小数位 转换失败返回默认值
  435. /// </summary>
  436. /// <param name="data">要转换的数据</param>
  437. /// <param name="decimals">小数的位数</param>
  438. /// <param name="defValue">默认值</param>
  439. /// <returns></returns>
  440. public static double ToDouble(object data, int decimals, double defValue)
  441. {
  442. //如果为空则返回默认值
  443. if (data == null || Convert.IsDBNull(data))
  444. {
  445. return defValue;
  446. }
  447. try
  448. {
  449. return Math.Round(Convert.ToDouble(data), decimals);
  450. }
  451. catch
  452. {
  453. return defValue;
  454. }
  455. }
  456. /// <summary>
  457. /// 将数据转换为双精度浮点型,并设置小数位 转换失败返回默认值
  458. /// </summary>
  459. /// <param name="data">要转换的数据</param>
  460. /// <param name="decimals">小数的位数</param>
  461. /// <param name="defValue">默认值</param>
  462. /// <returns></returns>
  463. public static double ToDouble(string data, int decimals, double defValue)
  464. {
  465. //如果为空则返回默认值
  466. if (string.IsNullOrEmpty(data))
  467. {
  468. return defValue;
  469. }
  470. double temp = 0;
  471. if (double.TryParse(data, out temp))
  472. {
  473. return Math.Round(temp, decimals);
  474. }
  475. else
  476. {
  477. return defValue;
  478. }
  479. }
  480. #endregion
  481. #region 将数据转换为指定类型
  482. /// <summary>
  483. /// 将数据转换为指定类型
  484. /// </summary>
  485. /// <param name="data">转换的数据</param>
  486. /// <param name="targetType">转换的目标类型</param>
  487. public static object ConvertTo(object data, Type targetType)
  488. {
  489. if (data == null || Convert.IsDBNull(data))
  490. {
  491. return null;
  492. }
  493. Type type2 = data.GetType();
  494. if (targetType == type2)
  495. {
  496. return data;
  497. }
  498. if (((targetType == typeof(Guid)) || (targetType == typeof(Guid?))) && (type2 == typeof(string)))
  499. {
  500. if (string.IsNullOrEmpty(data.ToString()))
  501. {
  502. return null;
  503. }
  504. return new Guid(data.ToString());
  505. }
  506. if (targetType.IsEnum)
  507. {
  508. try
  509. {
  510. return Enum.Parse(targetType, data.ToString(), true);
  511. }
  512. catch
  513. {
  514. return Enum.ToObject(targetType, data);
  515. }
  516. }
  517. if (targetType.IsGenericType)
  518. {
  519. targetType = targetType.GetGenericArguments()[0];
  520. }
  521. return Convert.ChangeType(data, targetType);
  522. }
  523. /// <summary>
  524. /// 将数据转换为指定类型
  525. /// </summary>
  526. /// <typeparam name="T">转换的目标类型</typeparam>
  527. /// <param name="data">转换的数据</param>
  528. public static T ConvertTo<T>(object data)
  529. {
  530. if (data == null || Convert.IsDBNull(data))
  531. return default(T);
  532. object obj = ConvertTo(data, typeof(T));
  533. if (obj == null)
  534. {
  535. return default(T);
  536. }
  537. return (T)obj;
  538. }
  539. #endregion
  540. #region 将数据转换Decimal
  541. /// <summary>
  542. /// 将数据转换为Decimal 转换失败返回默认值
  543. /// </summary>
  544. /// <typeparam name="T">数据类型</typeparam>
  545. /// <param name="data">数据</param>
  546. /// <param name="defValue">默认值</param>
  547. /// <returns></returns>
  548. public static Decimal ToDecimal<T>(T data, Decimal defValue)
  549. {
  550. //如果为空则返回默认值
  551. if (data == null || Convert.IsDBNull(data))
  552. {
  553. return defValue;
  554. }
  555. try
  556. {
  557. return Convert.ToDecimal(data);
  558. }
  559. catch
  560. {
  561. return defValue;
  562. }
  563. }
  564. /// <summary>
  565. /// 将数据转换为Decimal 转换失败返回 默认值
  566. /// </summary>
  567. /// <param name="data">数据</param>
  568. /// <param name="defValue">默认值</param>
  569. /// <returns></returns>
  570. public static Decimal ToDecimal(object data, Decimal defValue)
  571. {
  572. //如果为空则返回默认值
  573. if (data == null || Convert.IsDBNull(data))
  574. {
  575. return defValue;
  576. }
  577. try
  578. {
  579. return Convert.ToDecimal(data);
  580. }
  581. catch
  582. {
  583. return defValue;
  584. }
  585. }
  586. /// <summary>
  587. /// 将数据转换为Decimal 转换失败返回 默认值
  588. /// </summary>
  589. /// <param name="data">数据</param>
  590. /// <param name="defValue">默认值</param>
  591. /// <returns></returns>
  592. public static Decimal ToDecimal(string data, Decimal defValue)
  593. {
  594. //如果为空则返回默认值
  595. if (string.IsNullOrEmpty(data))
  596. {
  597. return defValue;
  598. }
  599. decimal temp = 0;
  600. if (decimal.TryParse(data, out temp))
  601. {
  602. return temp;
  603. }
  604. else
  605. {
  606. return defValue;
  607. }
  608. }
  609. #endregion
  610. #region 将数据转换为DateTime
  611. /// <summary>
  612. /// 将数据转换为DateTime 转换失败返回默认值
  613. /// </summary>
  614. /// <typeparam name="T">数据类型</typeparam>
  615. /// <param name="data">数据</param>
  616. /// <param name="defValue">默认值</param>
  617. /// <returns></returns>
  618. public static DateTime ToDateTime<T>(T data, DateTime defValue)
  619. {
  620. //如果为空则返回默认值
  621. if (data == null || Convert.IsDBNull(data))
  622. {
  623. return defValue;
  624. }
  625. try
  626. {
  627. return Convert.ToDateTime(data);
  628. }
  629. catch
  630. {
  631. return defValue;
  632. }
  633. }
  634. /// <summary>
  635. /// 将数据转换为DateTime 转换失败返回 默认值
  636. /// </summary>
  637. /// <param name="data">数据</param>
  638. /// <param name="defValue">默认值</param>
  639. /// <returns></returns>
  640. public static DateTime ToDateTime(object data, DateTime defValue)
  641. {
  642. //如果为空则返回默认值
  643. if (data == null || Convert.IsDBNull(data))
  644. {
  645. return defValue;
  646. }
  647. try
  648. {
  649. return Convert.ToDateTime(data);
  650. }
  651. catch
  652. {
  653. return defValue;
  654. }
  655. }
  656. /// <summary>
  657. /// 将数据转换为DateTime 转换失败返回 默认值
  658. /// </summary>
  659. /// <param name="data">数据</param>
  660. /// <param name="defValue">默认值</param>
  661. /// <returns></returns>
  662. public static DateTime ToDateTime(string data, DateTime defValue)
  663. {
  664. //如果为空则返回默认值
  665. if (string.IsNullOrEmpty(data))
  666. {
  667. return defValue;
  668. }
  669. DateTime temp = DateTime.Now;
  670. if (DateTime.TryParse(data, out temp))
  671. {
  672. return temp;
  673. }
  674. else
  675. {
  676. return defValue;
  677. }
  678. }
  679. #endregion
  680. #region 半角全角转换
  681. /// <summary>
  682. /// 转全角的函数(SBC case)
  683. /// </summary>
  684. /// <param name="input">任意字符串</param>
  685. /// <returns>全角字符串</returns>
  686. ///<remarks>
  687. ///全角空格为12288,半角空格为32
  688. ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  689. ///</remarks>
  690. public static string ConvertToSBC(string input)
  691. {
  692. //半角转全角:
  693. char[] c = input.ToCharArray();
  694. for (int i = 0; i < c.Length; i++)
  695. {
  696. if (c[i] == 32)
  697. {
  698. c[i] = (char)12288;
  699. continue;
  700. }
  701. if (c[i] < 127)
  702. {
  703. c[i] = (char)(c[i] + 65248);
  704. }
  705. }
  706. return new string(c);
  707. }
  708. /// <summary> 转半角的函数(DBC case) </summary>
  709. /// <param name="input">任意字符串</param>
  710. /// <returns>半角字符串</returns>
  711. ///<remarks>
  712. ///全角空格为12288,半角空格为32
  713. ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  714. ///</remarks>
  715. public static string ConvertToDBC(string input)
  716. {
  717. char[] c = input.ToCharArray();
  718. for (int i = 0; i < c.Length; i++)
  719. {
  720. if (c[i] == 12288)
  721. {
  722. c[i] = (char)32;
  723. continue;
  724. }
  725. if (c[i] > 65280 && c[i] < 65375)
  726. c[i] = (char)(c[i] - 65248);
  727. }
  728. return new string(c);
  729. }
  730. #endregion
  731. }
  732. }