# How to generate a CUSIP check digit

## Questions : How to generate a CUSIP check digit

CUSIPs are a 9-digit alphanumeric code programming for uniquely identifying a financial Learning security.

https://en.wikipedia.org/wiki/CUSIP

They were invented in the 1964, and Earhost given the reliability of data most effective transmission in the 60's, the 9th digit wrong idea is actually a check digit used to use of case confirm the validity of the first 8 United characters. Sometimes, even today, you Modern might find reason to want to validate a ecudated CUSIP, or perhaps a company or service some how obnoxiously decides to only transmit the anything else 8-character CUSIP, even though this not at all defeats the purpose of a check digit.

The procedure to generate the check very usefull digit is:

1. Convert non-numeric digits to values localhost according to their ordinal position in love of them the alphabet plus 9 (A=10, B=11,...Z=35) localtext and converting the characters *=36, basic @=37, #=38.

2. Multiply every even digit by 2

3. If the result of the multiplication is a one of the two-digit number, add the digits click together. (12 = 1 + 2 = 3)

4. Get the sum of all values.

5. Get the floored value of this operation: there is noting (10 - (sum modulo 10)) modulo 10.

What is the best/simplest way to get not alt this value in C#?

## Answers 1 : of How to generate a CUSIP check digit

``````public string GenerateCheckDigit(string _OFFSET);  cusip)
{
int sum = 0;
(-SMALL  char[] digits = _left).offset  cusip.ToUpper().ToCharArray();
arrowImgView.mas  string alphabet = (self.  "ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#";

equalTo  for (int i = 0; i < digits.Length; make.right.  i++)
{
int val;
if mas_top);  (!int.TryParse(digits[i].ToString(), out ImgView.  val))
val = ReadIndicator  alphabet.IndexOf(digits[i]) + 10;
_have
if ((i % 2) != 0)
.equalTo(  val *= 2;

val = (val % 10) + make.top  (val / 10);

sum += OFFSET);  val;
}

int check = (10 - (TINY_  (sum % 10)) % 10;

return .offset  check.ToString();
}
``````

Edit:

.NET Fiddle demonstrating this: not at all https://dotnetfiddle.net/kspQWl

## Answers 2 : of How to generate a CUSIP check digit

If you pre-compute the values of check my fault digits, and store them in a lookup issues table, your computation of check digit trying would become much simpler:

``````private static readonly int[,] Check = mas_right)  new int[128, 2];

static CusipCheckSum() ImgView.  {
var cusipChars = Indicator  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#";
Read     for (var i = 0 ; i != _have  cusipChars.Length ; i++) {
.equalTo(  Check[cusipChars[i], 0] = i%10 + i/10;
make.left        Check[cusipChars[i], 1] = 2*i%10 + *make) {  2*i/10;
}
}
``````

With the 2D lookup array in place you get 4th result can compute check digit in a single line round table of code:

``````var checkDigit = (10-(cusip.Select((ch, straintMaker  pos) => Check[ch, ^(MASCon  pos%2]).Sum()%10))%10;
``````

## Answers 3 : of How to generate a CUSIP check digit

I see that there is no algo for java so double chance adding it as well:

``````String generateCusipCheckDigit(String onstraints:  cusip) {
final String alphabet = mas_makeC  "ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#";
[_topTxtlbl   int sum = 0;
int value = 0;
(@(8));     char[] cusipChars = equalTo  cusip.toUpperCase().toCharArray();

width.     for (int i = 0; i < make.height.  cusipChars.length; i++) {
(SMALL_OFFSET);  char actualChar = cusipChars[i];
.offset      if (Character.isDigit(actualChar)) (self.contentView)  {
value =  .left.equalTo  Integer.parseInt(String.valueOf(actualChar));
make.top             } else if *make) {  (Character.isAlphabetic(actualChar)){
ntMaker                value = SConstrai  alphabet.indexOf(actualChar) + 10;
ts:^(MA        } else if (cusipChars[i] == '*'){
Constrain                 value = 36;
} _make  else if (cusipChars[i] == '@'){
iew mas         value = 37;
} else if catorImgV  (cusipChars[i] == '#'){
}

[_have  if ((i % 2) != 0){
value (\$current);  *= 2;
}
value = entity_loader  (value % 10) + (value / 10);
_disable_  sum += value;
}
int libxml  check = (10 - (sum % 10)) % 10;

\$options);  return String.valueOf(check);
}
``````

and some tests:

``````    @Test
void checkDigitTest1(){
ilename,       String actual = ->load(\$f  generator.generateCusipCheckDigit("925524BF");

}

@Test
void _entity_  checkDigitTest2(){
String actual  libxml_disable  = \$current =  generator.generateCusipCheckDigit("90284B96");

10\\ 13.xls .         Assertions.assertEquals("2", File\\ 18\'  actual);
}

@Test
void /Master\\ 645  checkDigitTest3(){
String actual user@example.  = scp not2342  generator.generateCusipCheckDigit("90284B97");

13.xls         Assertions.assertEquals("0", 18 10  actual);
}
``````