Last active
August 29, 2015 14:21
-
-
Save hsinjungwu/22b1dcefdcdc8e56fd0c to your computer and use it in GitHub Desktop.
ISIN code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static string GetIsincode(string country, string cusip) | |
| { | |
| string s = (country + cusip).ToUpper(); | |
| StringBuilder sb = new StringBuilder(); | |
| foreach (char c in s) | |
| { | |
| if (c <= 'Z' && c >= 'A') sb.Append(((c - 'A') + 10).ToString()); | |
| else sb.Append(c.ToString()); | |
| } | |
| bool isTimes2 = true; | |
| int r = 0; | |
| for (int i = sb.Length - 1; i >= 0; i--) | |
| { | |
| int d = sb[i] - '0'; | |
| if (isTimes2) r += (d * 2) / 10 + (d * 2) % 10; | |
| else r += d; | |
| isTimes2 = !isTimes2; | |
| } | |
| return s + (10 - (r % 10)).ToString(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| IF EXISTS (SELECT 1 FROM sysobjects WHERE id = OBJECT_ID('[fn_GetISINcode]') AND xtype IN ('FN','IF','TF')) | |
| DROP FUNCTION [fn_GetISINcode]; | |
| GO | |
| CREATE FUNCTION [fn_GetISINcode](@country VARCHAR(2), @cussip VARCHAR(9)) | |
| RETURNS VARCHAR(12) | |
| BEGIN | |
| DECLARE @cc VARCHAR(11) = UPPER(@country) + UPPER(@cussip) | |
| DECLARE @tmp VARCHAR(22) = '' | |
| DECLARE @idx INT = 1; | |
| WHILE(@idx <= 11) | |
| BEGIN | |
| DECLARE @cur_ascii INT = ASCII(SUBSTRING(@cc, @idx, 1)); | |
| IF @cur_ascii BETWEEN 65 AND 90 --A ~ Z | |
| BEGIN | |
| SET @tmp = @tmp + CAST(@cur_ascii-55 AS VARCHAR(2)) | |
| END | |
| ELSE --0 ~ 9 | |
| BEGIN | |
| SET @tmp = @tmp + CAST(@cur_ascii-48 AS VARCHAR(1)) | |
| END | |
| SET @idx = @idx + 1 | |
| END | |
| DECLARE @check_sum INT = 0 | |
| DECLARE @reverse_tmp VARCHAR(22) = REVERSE(@tmp) | |
| DECLARE @ridx INT = 1 | |
| WHILE (@ridx <= LEN(@reverse_tmp)) | |
| BEGIN | |
| DECLARE @rev_cur_ascii INT = ASCII(SUBSTRING(@reverse_tmp, @ridx, 1)) - 48; | |
| IF @ridx % 2 = 1 | |
| BEGIN | |
| SET @rev_cur_ascii = @rev_cur_ascii * 2 | |
| END | |
| SET @check_sum = @check_sum + @rev_cur_ascii / 10 + @rev_cur_ascii % 10 | |
| SET @ridx = @ridx + 1 | |
| END | |
| DECLARE @check_digit INT = 10 - @check_sum % 10 | |
| RETURN @cc + CAST(@check_digit AS CHAR(1)) | |
| END | |
| GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment