Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i will like to break a 18 digit number into parts and dont know how to go about it.

for example
num=123456789123654258 //18 digit number

num consist of 5 joint parts namely

braCode=123, //1st 3 digit
cusCode=4567891, //from 4th to 10th digit
curCode=23, //11th and 12th digit
ledcode=6542, //13th to 16th digit
subCode=58, // 17th and 18th.

how do i get bracode,cuscode,curcode,ledcode and subcode from num.

What I have tried:

i can get the following
bracode = num.Substring(0,3)
subCode= num.Substring(16);
curcode= ?
ledcode= ?
cuscode= ?
Posted
Updated 20-Sep-16 8:20am
v2

long num=123456789123654258;
int subcode = num%100;
num = Math.Floor(num/100);
int ledcode = num%10000;
num = Math.Floor(num/10000);
int curCode = num%100;
num = Math.Floor(num/100);
int cusCode = num%10000000;
int braCode = Math.Floor(num/10000000)
 
Share this answer
 
The first question is: "Are you given the 18 digit number as a type long, or as a string?"
If as long, then Solution 3 by Nipurba Konar is on the right track but it can be simplified:
C#
long subCode = Math.DivRem(num, 100, out num);
long ledcode = Math.DivRem(num, 10000, out num);
long curCode = Math.DivRem(num, 100, out num);
long cusCode = Math.DivRem(num, 10000000, out num);
long braCode = num;

If you have a string, then a Regex is pretty straight forward:
C#
class Program
{
  static readonly Regex TakeApart = new Regex("^(...)(.......)(..)(....)(..)$", RegexOptions.Compiled);
  static void Main(string[] args)
  {
    string num = "123456789123654258";
    Match m = TakeApart.Match(num);
    if (!m.Success)
    {
      // something went wrong!
    }
    string braCode = m.Groups[1].Value; //1st 3 digits
    string cusCode = m.Groups[2].Value; //from 4th to 10th digit
    string curCode = m.Groups[3].Value; //11th and 12th digit
    string ledcode = m.Groups[4].Value; //13th to 16th digit
    string subCode = m.Groups[5].Value; // 17th and 18th.
    // proceed ....
  }
}

This gives 5 strings as output.
 
Share this answer
 
Comments
Leo Chapiro 20-Sep-16 14:24pm    
+5
Try smth like this:

try
            {
                long nNum = 123456789123654258;
                string sNum = nNum.ToString();

                int braCode = Int32.Parse(sNum.Substring(0, 3));    //123 //from 4th to 10th digit
                int cusCode = Int32.Parse(sNum.Substring(3, 7));    //4567891, from 4th to 10th digit
                int curCode = Int32.Parse(sNum.Substring(10, 2));   //23, //11th and 12th digit
                int ledcode = Int32.Parse(sNum.Substring(12, 4));   //6542, //13th to 16th digit
                int subCode = Int32.Parse(sNum.Substring(16, 2));   //58, // 17th and 18th.
            }
           catch(Exception ex)
            {
               // do smth!
            }
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 20-Sep-16 9:28am    
5
Leo Chapiro 20-Sep-16 9:43am    
Thank you! :)
You are on the right track with substring - like in your first example, just with different offsets Substring(0, 3), Substring(3, 7), substring(10, 2) and so on.
 
Share this answer
 
Your question is inconsistent:
Here you have numbers:
C#
num=123456789123654258 //18 digit number
...
braCode=123, //1st 3 digit
cusCode=4567891, //from 4th to 10th digit
curCode=23, //11th and 12th digit
ledcode=6542, //13th to 16th digit
subCode=58, // 17th and 18th.

and here it is strings:
C#
bracode = num.Substring(0,3)
subCode= num.Substring(16);

and you don't claim any problem, which is surprising with C# !
In the 8 lines of code, only 1 can compile, just because of the endings.
If:
C#
bracode = num.Substring(0,3)

works, you know the solution, the next will be
C#
cusCode = num.Substring(3,7)

Otherwise the easiest is to break the number starting by the least significant digits
C#
subcode= num % 100
tmp= num / 100
ledcode= tmp % 10000
tmp= tmp / 10000
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900