You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.6 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Shentun.Utilities
  5. {
  6. public static class ConvertExtr
  7. {
  8. public static int ToAge(DateTime birthdate)
  9. {
  10. if(birthdate == null)
  11. {
  12. throw new ArgumentNullException("转换到年龄时出生日期不能为空");
  13. }
  14. DateTime now = DateTime.Now;
  15. int age = now.Year - birthdate.Year;
  16. if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day))
  17. {
  18. age--;
  19. }
  20. return age < 0 ? 0 : age;
  21. }
  22. public static DateTime ToBirthDateByIdNo(string idNo)
  23. {
  24. if (string.IsNullOrWhiteSpace(idNo))
  25. throw new Exception("身份证号不能为空");
  26. if (idNo.Length != 18)
  27. throw new Exception("身份证长度必须为18位");
  28. var birthDate = Convert.ToDateTime(idNo.Substring(6, 4) + "-" +
  29. idNo.Substring(10, 2) + "-" +
  30. idNo.Substring(12, 2));
  31. if (birthDate < new DateTime(1880, 1, 1))
  32. {
  33. throw new Exception("身份证号中的出生日期不正确");
  34. }
  35. return birthDate;
  36. }
  37. public static DateTime ToBirthDateByAge(short age)
  38. {
  39. if (age <= 0)
  40. throw new Exception("转换成出生日期时年龄不能小于等于0");
  41. var birthDate = DateTime.Now.AddYears(-age);
  42. if (birthDate < new DateTime(1880, 1, 1))
  43. {
  44. throw new Exception("年龄转换出的出生日期不正确");
  45. }
  46. return birthDate;
  47. }
  48. public static string ToSexByIdNo(string idNo)
  49. {
  50. if (string.IsNullOrWhiteSpace(idNo))
  51. throw new Exception("身份证号不能为空");
  52. if (idNo.Length != 18)
  53. throw new Exception("身份证长度必须为18位");
  54. string sex = idNo.Substring(14, 3);
  55. if (int.Parse(sex) % 2 == 0)
  56. {
  57. sex = Sex.Female;
  58. }
  59. else if (int.Parse(sex) % 2 != 0)
  60. {
  61. sex = Sex.Male;
  62. }
  63. return sex;
  64. }
  65. public static string ToBirthPlaceByIdNo(string idNo)
  66. {
  67. if (string.IsNullOrWhiteSpace(idNo))
  68. throw new Exception("身份证号不能为空");
  69. if (idNo.Length != 18)
  70. throw new Exception("身份证长度必须为18位");
  71. var birthPlace = idNo.Substring(0, 6);
  72. return birthPlace;
  73. }
  74. public static string ToStringByDateOnly(DateOnly? dateOnly)
  75. {
  76. if (dateOnly == null)
  77. {
  78. return "0000-00-00";
  79. }
  80. return ((DateOnly)dateOnly).ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
  81. }
  82. public static string StringToASCII(string value)
  83. {
  84. System.Text.Encoding gbk_encoder = System.Text.Encoding.GetEncoding("gb2312");
  85. byte[] bytes = gbk_encoder.GetBytes(value);
  86. string textAscii = string.Empty;
  87. for (int i = 0; i < bytes.Length; i++)
  88. {
  89. textAscii += bytes[i].ToString() + ",";
  90. }
  91. return textAscii.TrimEnd(',');
  92. }
  93. public static string ToChString(string str)
  94. {
  95. System.Text.Encoding gbk_encoder = System.Text.Encoding.GetEncoding("gb2312");
  96. byte[] bs = gbk_encoder.GetBytes(str);
  97. char[] cs = new char[bs.Length];
  98. for (int i = 0; i < bs.Length; i++)
  99. cs[i] = Convert.ToChar(bs[i]);
  100. //return Encoding.GetEncoding("gb2312").GetString(bs, 0, bs.Length);
  101. //return Convert.ToString(bs);
  102. return new String(cs);
  103. }
  104. public static string ToGB2312(int codepage, string str)
  105. {
  106. try
  107. {
  108. Encoding cp850 = Encoding.GetEncoding(codepage);
  109. Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
  110. byte[] temp = cp850.GetBytes(str);
  111. return Encoding.GetEncoding("gb2312").GetString(temp, 0, temp.Length);
  112. }
  113. catch (Exception ex)//(UnsupportedEncodingException ex)
  114. {
  115. Console.Write(ex.Message);
  116. return null;
  117. }
  118. }
  119. public static string ToGB2312(string str)
  120. {
  121. byte[] bs = Encoding.Default.GetBytes(str);
  122. bs = Encoding.Convert(Encoding.Default, Encoding.GetEncoding("gb2312"), bs);
  123. return Encoding.GetEncoding("gb2312").GetString(bs);
  124. //Encoding cp850 = Encoding.Default;
  125. //Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
  126. //byte[] temp = cp850.GetBytes(str);
  127. //return Encoding.GetEncoding("gb2312").GetString(temp, 0, temp.Length);
  128. }
  129. public static string GetHexFromChs(string s)
  130. {
  131. if ((s.Length % 2) != 0)
  132. {
  133. s += " ";//空格
  134. //throw new ArgumentException("s is not valid chinese string!");
  135. }
  136. System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
  137. byte[] bytes = chs.GetBytes(s);
  138. string str = "";
  139. for (int i = 0; i < bytes.Length; i++)
  140. {
  141. str += string.Format("{0:X}", bytes[i]);
  142. }
  143. return str;
  144. }
  145. }
  146. }