/// <summary> /// 解析身份证文本字节 /// </summary> /// <param name="idCardTextBytes"></param> /// <returns></returns> protected static IDCardData ParseIDCardTextBytes(byte[] idCardTextBytes) { if (idCardTextBytes.isEmpty()) { throw new ArgumentNullException(nameof(idCardTextBytes)); } if (idCardTextBytes.Length < 256) { throw new ArgumentException("invalid idcard text bytes", nameof(idCardTextBytes)); } var cardData = new IDCardData(); cardData.Name = ConvertIDCardBytesToUTF8String(idCardTextBytes, 0, 30).Trim(); cardData.Sex = TypeHelper.parseInt(ConvertIDCardBytesToUTF8String(idCardTextBytes, 30, 2)); cardData.Nation = TypeHelper.parseInt(ConvertIDCardBytesToUTF8String(idCardTextBytes, 32, 4)); cardData.Birthday = ConvertIDCardBytesToUTF8String(idCardTextBytes, 36, 16).Trim(); cardData.Address = ConvertIDCardBytesToUTF8String(idCardTextBytes, 52, 70).Trim(); cardData.IDCardNo = ConvertIDCardBytesToUTF8String(idCardTextBytes, 122, 36).Trim(); cardData.IssuingAuthority = ConvertIDCardBytesToUTF8String(idCardTextBytes, 158, 30).Trim(); cardData.ValidBeginDate = ConvertIDCardBytesToUTF8String(idCardTextBytes, 188, 16).Trim(); cardData.ValidEndDate = ConvertIDCardBytesToUTF8String(idCardTextBytes, 204, 16).Trim(); return(cardData); }
/// <summary> /// 格式化身份证信息 /// </summary> /// <param name="cardData"></param> /// <returns></returns> public static IDCardInfo FormatCardInfo(this IDCardData cardData) { if (cardData == null) { throw new ArgumentNullException(nameof(cardData)); } return(new IDCardInfo { Name = cardData.Name, Sex = GetSexString(cardData.Sex), Nation = GetNationString(cardData.Nation), Birthday = ParseIDCardDateString(cardData.Birthday), Address = cardData.Address, IDCardNo = cardData.IDCardNo, IssuingAuthority = cardData.IssuingAuthority, ValidBeginDate = ParseIDCardDateString(cardData.ValidBeginDate), ValidEndDate = ParseIDCardValidEndDateString(cardData.ValidEndDate) }); }