System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); functionality
-
Hi, I get a program c# when googling that validates the IBAN :
static bool isIban (string code_iban)
{
// 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
code_iban = code_iban.ToUpper () ;
StringBuilder sb = new StringBuilder ();
foreach ( char car in code_iban)
{
if (char.IsDigit (car) || char.IsLetter (car))
sb.Append (car);
}
code_iban = sb.ToString ();
if (code_iban.Length < 15 || code_iban.Length > 34)
return false;
// 2. Déplacer les 4 premiers caractères à droite
string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
// 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
StringBuilder sb2 = new StringBuilder ();
foreach (char c in iban)
{
int entier;
if (char.IsLetter(c))
entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
else
entier = Convert.ToChar (c) - Convert.ToChar ("0");
sb2.Append (entier);
}
string checkSumString = sb2.ToString();
// 4. Diviser le nombre ainsi obtenu par 97.
int checksum = int.Parse(checkSumString.Substring(0, 1));
int i = 1;
while (i < checkSumString.Length)
{
int v = int.Parse(checkSumString.Substring(i, 1));
checksum *= 10;
checksum += v;
checksum = checksum % 97;
System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
}
if (checksum == 1)
return true;
else
return false;
}
}I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty
Well, Math.Max is retrieving the maximum of i or i-1. Interlocked.Increment is used to increment i and store it all as an atomic action.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Well, Math.Max is retrieving the maximum of i or i-1. Interlocked.Increment is used to increment i and store it all as an atomic action.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
Hi, If u can give me the // algorithme of this.I can do this like this simply
Math.Max( ref i , i-1)
i ++;why using of Interlocked.Increment??????? ty
-
Hi, I get a program c# when googling that validates the IBAN :
static bool isIban (string code_iban)
{
// 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
code_iban = code_iban.ToUpper () ;
StringBuilder sb = new StringBuilder ();
foreach ( char car in code_iban)
{
if (char.IsDigit (car) || char.IsLetter (car))
sb.Append (car);
}
code_iban = sb.ToString ();
if (code_iban.Length < 15 || code_iban.Length > 34)
return false;
// 2. Déplacer les 4 premiers caractères à droite
string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
// 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
StringBuilder sb2 = new StringBuilder ();
foreach (char c in iban)
{
int entier;
if (char.IsLetter(c))
entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
else
entier = Convert.ToChar (c) - Convert.ToChar ("0");
sb2.Append (entier);
}
string checkSumString = sb2.ToString();
// 4. Diviser le nombre ainsi obtenu par 97.
int checksum = int.Parse(checkSumString.Substring(0, 1));
int i = 1;
while (i < checkSumString.Length)
{
int v = int.Parse(checkSumString.Substring(i, 1));
checksum *= 10;
checksum += v;
checksum = checksum % 97;
System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
}
if (checksum == 1)
return true;
else
return false;
}
}I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty
This line:
System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
seems utterly stupid to me. Comparing a number which is increased to what it was before and taking the maximum will always return the increased number. Even so, nothing is done with the return value. And why is this increase done using the interlocked class? Doesn't look terribly multi-threaded to me and if it is then you need additional locking.
i++
is probably what you want, and I would expect the loop initialisation to seti = 0
, rather thani = 1
on casual inspection.Regards, Rob Philpott.
-
Hi, I get a program c# when googling that validates the IBAN :
static bool isIban (string code_iban)
{
// 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
code_iban = code_iban.ToUpper () ;
StringBuilder sb = new StringBuilder ();
foreach ( char car in code_iban)
{
if (char.IsDigit (car) || char.IsLetter (car))
sb.Append (car);
}
code_iban = sb.ToString ();
if (code_iban.Length < 15 || code_iban.Length > 34)
return false;
// 2. Déplacer les 4 premiers caractères à droite
string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
// 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
StringBuilder sb2 = new StringBuilder ();
foreach (char c in iban)
{
int entier;
if (char.IsLetter(c))
entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
else
entier = Convert.ToChar (c) - Convert.ToChar ("0");
sb2.Append (entier);
}
string checkSumString = sb2.ToString();
// 4. Diviser le nombre ainsi obtenu par 97.
int checksum = int.Parse(checkSumString.Substring(0, 1));
int i = 1;
while (i < checkSumString.Length)
{
int v = int.Parse(checkSumString.Substring(i, 1));
checksum *= 10;
checksum += v;
checksum = checksum % 97;
System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
}
if (checksum == 1)
return true;
else
return false;
}
}I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty
it doesn't make sense at all;
i
is a local variable, no other thread can touch it, so the whole statement boils down toi++
and the while loop should have been a for loop. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hi, If u can give me the // algorithme of this.I can do this like this simply
Math.Max( ref i , i-1)
i ++;why using of Interlocked.Increment??????? ty
You shouldn't. In this case, it's absolutely no use whatsoever - frankly, it looks like somebody is trying to be too clever for their own good. Interlocked increment only makes sense in multi-threaded scenarios where there is a possibility that somebody else may increment a shared variable. As i is local, it's pointless. This code looks like obfuscation through stupidity.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Hi, I get a program c# when googling that validates the IBAN :
static bool isIban (string code_iban)
{
// 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
code_iban = code_iban.ToUpper () ;
StringBuilder sb = new StringBuilder ();
foreach ( char car in code_iban)
{
if (char.IsDigit (car) || char.IsLetter (car))
sb.Append (car);
}
code_iban = sb.ToString ();
if (code_iban.Length < 15 || code_iban.Length > 34)
return false;
// 2. Déplacer les 4 premiers caractères à droite
string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
// 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
StringBuilder sb2 = new StringBuilder ();
foreach (char c in iban)
{
int entier;
if (char.IsLetter(c))
entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
else
entier = Convert.ToChar (c) - Convert.ToChar ("0");
sb2.Append (entier);
}
string checkSumString = sb2.ToString();
// 4. Diviser le nombre ainsi obtenu par 97.
int checksum = int.Parse(checkSumString.Substring(0, 1));
int i = 1;
while (i < checkSumString.Length)
{
int v = int.Parse(checkSumString.Substring(i, 1));
checksum *= 10;
checksum += v;
checksum = checksum % 97;
System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
}
if (checksum == 1)
return true;
else
return false;
}
}I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty
-
Yes, however this time there was a whole new sentence, and a big code snip, so we could provide more relevant comments. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Yes, however this time there was a whole new sentence, and a big code snip, so we could provide more relevant comments. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
And he is using C# not JavaScript :-D
I know the language. I've read a book. - _Madmatt
-
And he is using C# not JavaScript :-D
I know the language. I've read a book. - _Madmatt
He is flexible for now, in the end he'll really want JavaScript code doing the same. Just you wait. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
You shouldn't. In this case, it's absolutely no use whatsoever - frankly, it looks like somebody is trying to be too clever for their own good. Interlocked increment only makes sense in multi-threaded scenarios where there is a possibility that somebody else may increment a shared variable. As i is local, it's pointless. This code looks like obfuscation through stupidity.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
Pete O'Hanlon wrote:
obfuscation through stupidity
:laugh: 5
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)