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

using System;
using System.Collections.Generic;
using System.Text;
using TencentCloud.Ame.V20190916.Models;
namespace Shentun.Utilities
{
public static class ConvertExtr
{
public static int ToAge(DateTime birthdate)
{
if(birthdate == null)
{
throw new ArgumentNullException("转换到年龄时出生日期不能为空");
}
DateTime now = DateTime.Now;
int age = now.Year - birthdate.Year;
if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day))
{
age--;
}
return age < 0 ? 0 : age;
}
public static DateTime ToBirthDateByIdNo(string idNo)
{
if (string.IsNullOrWhiteSpace(idNo))
throw new Exception("身份证号不能为空");
if (idNo.Length != 18)
throw new Exception("身份证长度必须为18位");
var birthDate = Convert.ToDateTime(idNo.Substring(6, 4) + "-" +
idNo.Substring(10, 2) + "-" +
idNo.Substring(12, 2));
if (birthDate < new DateTime(1880, 1, 1))
{
throw new Exception("身份证号中的出生日期不正确");
}
return birthDate;
}
public static DateTime ToBirthDateByAge(short age)
{
if (age <= 0)
throw new Exception("转换成出生日期时年龄不能小于等于0");
var birthDate = DateTime.Now.AddYears(-age);
if (birthDate < new DateTime(1880, 1, 1))
{
throw new Exception("年龄转换出的出生日期不正确");
}
return birthDate;
}
public static string ToSexByIdNo(string idNo)
{
if (string.IsNullOrWhiteSpace(idNo))
throw new Exception("身份证号不能为空");
if (idNo.Length != 18)
throw new Exception("身份证长度必须为18位");
string sex = idNo.Substring(14, 3);
if (int.Parse(sex) % 2 == 0)
{
sex = Sex.Female;
}
else if (int.Parse(sex) % 2 != 0)
{
sex = Sex.Male;
}
return sex;
}
public static string ToBirthPlaceByIdNo(string idNo)
{
if (string.IsNullOrWhiteSpace(idNo))
throw new Exception("身份证号不能为空");
if (idNo.Length != 18)
throw new Exception("身份证长度必须为18位");
var birthPlace = idNo.Substring(0, 6);
return birthPlace;
}
public static string ToStringByDateOnly(DateOnly? dateOnly)
{
if (dateOnly == null)
{
return "0000-00-00";
}
return ((DateOnly)dateOnly).ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
public static string StringToASCII(string value)
{
System.Text.Encoding gbk_encoder = System.Text.Encoding.GetEncoding("gb2312");
byte[] bytes = gbk_encoder.GetBytes(value);
string textAscii = string.Empty;
for (int i = 0; i < bytes.Length; i++)
{
textAscii += bytes[i].ToString() + ",";
}
return textAscii.TrimEnd(',');
}
public static string ToChString(string str)
{
System.Text.Encoding gbk_encoder = System.Text.Encoding.GetEncoding("gb2312");
byte[] bs = gbk_encoder.GetBytes(str);
char[] cs = new char[bs.Length];
for (int i = 0; i < bs.Length; i++)
cs[i] = Convert.ToChar(bs[i]);
//return Encoding.GetEncoding("gb2312").GetString(bs, 0, bs.Length);
//return Convert.ToString(bs);
return new String(cs);
}
public static string ToGB2312(int codepage, string str)
{
try
{
Encoding cp850 = Encoding.GetEncoding(codepage);
Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
byte[] temp = cp850.GetBytes(str);
return Encoding.GetEncoding("gb2312").GetString(temp, 0, temp.Length);
}
catch (Exception ex)//(UnsupportedEncodingException ex)
{
Console.Write(ex.Message);
return null;
}
}
public static string ToGB2312(string str)
{
byte[] bs = Encoding.Default.GetBytes(str);
bs = Encoding.Convert(Encoding.Default, Encoding.GetEncoding("gb2312"), bs);
return Encoding.GetEncoding("gb2312").GetString(bs);
//Encoding cp850 = Encoding.Default;
//Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
//byte[] temp = cp850.GetBytes(str);
//return Encoding.GetEncoding("gb2312").GetString(temp, 0, temp.Length);
}
public static string GetHexFromChs(string s)
{
if ((s.Length % 2) != 0)
{
s += " ";//空格
//throw new ArgumentException("s is not valid chinese string!");
}
System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
byte[] bytes = chs.GetBytes(s);
string str = "";
for (int i = 0; i < bytes.Length; i++)
{
str += string.Format("{0:X}", bytes[i]);
}
return str;
}
}
}