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.

182 lines
5.7 KiB

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