tag:blogger.com,1999:blog-40601577465686311762024-03-04T23:42:02.709-08:00An Inka Coding4Funinka jhonhttp://www.blogger.com/profile/10451555135592926867noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-4060157746568631176.post-29697886898759225112012-07-07T12:04:00.003-07:002012-07-07T16:16:44.206-07:00Validation Character for Peruvian DNI - 2Following is the function that receives an eight character string, the DNI, and returns the validation character.
The C++ code is published at GitHub and can be downloaded <a href="https://github.com/inkacoder/pirhua/blob/master/dni.cpp" target="_blank">here</a>.
<pre class="brush:cpp;">char alphaValidationCharacter(char *DNI)
{
const char serie[] = "89456789";
char const *t = serie;
char *s = DNI;
int sum = 0, partial = 0;
while(*s!='\0')
{
partial = (*s-'0')* (*t-'0');
sum += partial;
s++;
t++;
}
int modulus = 0;
modulus = sum % 11;
return 'A' + (modulus==0?11:modulus) - 1;
} </pre>
The complete project can be downloaded from <a href="https://github.com/inkacoder/pirhua/tarball/master" target="_blank">here</a>inka jhonhttp://www.blogger.com/profile/10451555135592926867noreply@blogger.com2tag:blogger.com,1999:blog-4060157746568631176.post-196679461054886622012-06-16T22:42:00.002-07:002016-04-27T07:42:24.444-07:00Validation Character for Peruvian DNI - 1Well, as my first theme I want to tell you about the Peruvian National Identity Card, named DNI. The Peruvian DNI is given to all the peruvian nationals at the age of 18 and from some years ago is also given to all new born. To his parents, I mean.<br />
As you guess the Peruvian DNI has a number, an 8-digit number that you have to remember for the rest of your life. But if you look carefully, following the 8-digit number there´s a number or a letter, the validation character.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0pVohLtyufqBgkd0zxCkhOEDqnOuSQz0hPsqokWQn12iOK_32CXPvSJqan5RUXj4fUNDdcJIYk-nBwZ3D402xMwl9CoxcWK3BmaIk6iEiERBjez4ArW2TGtntOk_DSqiakYumNP6TF10/s1600/dni.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="48" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0pVohLtyufqBgkd0zxCkhOEDqnOuSQz0hPsqokWQn12iOK_32CXPvSJqan5RUXj4fUNDdcJIYk-nBwZ3D402xMwl9CoxcWK3BmaIk6iEiERBjez4ArW2TGtntOk_DSqiakYumNP6TF10/s320/dni.jpg" width="320" /></a></div>
<br />
I was googling for some time searching for the algorithm to calculate the validation character of the Peruvian DNI without success, so I decided to deduce it. My approach was this:<br />
"Consider a 9 characters length string where the last of this characters is calculated from first eight ones by an unknown algorithm you have to deduce the algorithm from a set of examples."<br />
The set of examples can be obtained from a public service <a href="https://cel.reniec.gob.pe/valreg/valreg.do?accion=ini" target="_blank">here</a>.<br />
I treated this problem like an encryption challenge, my first guess was that the algorithm is a modified form of modulus 11, and I was right. Then, with a set of 40 strings was easy to deduce the coeficients and the variations to the original modulus 11 algorithm.<br />
<br />
In my next post I'll show you the algoritm in C++. inka jhonhttp://www.blogger.com/profile/10451555135592926867noreply@blogger.com03142-3100 NW 57th Ave, Aeropuerto de Miami (MIA), Miami, Florida 33122, EEUU25.799891182088331 -80.288085937522.182317682088332 -85.341796937499993 29.41746468208833 -75.234374937500007