Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate integers in C#

Tags:

c#

Is there an inexpensive way to concatenate integers in csharp?

Example: 1039 & 7056 = 10397056

like image 294
CountCet Avatar asked Jun 18 '09 18:06

CountCet


People also ask

Can you concatenate an integer?

You can use the String concatenation operator + to concatenate integers to a string in a clear and concise manner. Note that the compiler implicitly constructs an intermediate StringBuilder object, append the integers, and then call the toString() method.

Can you concatenate an int to a string in C?

Use asprintf , strcat and strcpy Functions to Concatenate String and Int in C. The first step to concatenating int variable and the character string is to convert an integer to string. We utilize asprintf function to store the passed integer as a character string.

How do you concatenate numbers?

This means that you can no longer perform any math operations on them. To combine numbers, use the CONCATENATE or CONCAT, TEXT or TEXTJOIN functions, and the ampersand (&) operator. Notes: In Excel 2016, Excel Mobile, and Excel for the web, CONCATENATE has been replaced with the CONCAT function.

Can we concatenate in C?

In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.


2 Answers

If you can find a situation where this is expensive enough to cause any concern, I'll be very impressed:

int a = 1039; int b = 7056;  int newNumber = int.Parse(a.ToString() + b.ToString()) 

Or, if you want it to be a little more ".NET-ish":

int newNumber = Convert.ToInt32(string.Format("{0}{1}", a, b)); 

int.Parse is not an expensive operation. Spend your time worrying about network I/O and O^N regexes.

Other notes: the overhead of instantiating StringBuilder means there's no point if you're only doing a few concatenations. And very importantly - if you are planning to turn this back into an integer, keep in mind it's limited to ~2,000,000,000. Concatenating numbers gets very large very quickly, and possibly well beyond the capacity of a 32-bit int. (signed of course).

like image 117
Rex M Avatar answered Sep 22 '22 19:09

Rex M


I'm a bit late at the party, but recently I had to concatenate integers. With 0 < a,b < 10^9 it can be done quite fast.

static ulong concat(uint a, uint b) {     if (b < 10U) return 10UL * a + b;     if (b < 100U) return 100UL * a + b;     if (b < 1000U) return 1000UL * a + b;     if (b < 10000U) return 10000UL * a + b;     if (b < 100000U) return 100000UL * a + b;     if (b < 1000000U) return 1000000UL * a + b;     if (b < 10000000U) return 10000000UL * a + b;     if (b < 100000000U) return 100000000UL * a + b;     return 1000000000UL * a + b; } 

Edit: the version below might be interesting (platform target: x64).

static ulong concat(ulong a, uint b) {     const uint c0 = 10, c1 = 100, c2 = 1000, c3 = 10000, c4 = 100000,         c5 = 1000000, c6 = 10000000, c7 = 100000000, c8 = 1000000000;     a *= b < c0 ? c0 : b < c1 ? c1 : b < c2 ? c2 : b < c3 ? c3 :          b < c4 ? c4 : b < c5 ? c5 : b < c6 ? c6 : b < c7 ? c7 : c8;     return a + b; } 
like image 41
P_P Avatar answered Sep 23 '22 19:09

P_P