CountCet
CountCet

Reputation: 4573

Concatenate integers in C#

Is there an inexpensive way to concatenate integers in csharp?

Example: 1039 & 7056 = 10397056

Upvotes: 44

Views: 89729

Answers (13)

Michael Stum
Michael Stum

Reputation: 181004

Not really inexpensive, but:

string con = string.Format("{0}{1}",int1,int2);

or

string con = int1.ToString() + int2.ToString();

If you use this in a loop, I think I would use Option 1, which uses a StringBuilder internally.

Upvotes: 1

João Neves
João Neves

Reputation: 53

I have this solution:

const int left = 1039;
const int right = 7056;

int rightLength = 10;

while (rightLength <= right)
{
    rightLength *= 10;
}

return (left * rightLength) + right;

Essentially it's adding 0's to the end of the first number and then adding the second one to it. Like so:

1039 * 10000 = 10390000
10390000 + 7056 = 10397056

I've also ran some benchmarkings with BenchmarkDotNet. Bellow are the test cases and summary of the results:

    public class Benchmarks
    {
        private const int left = 1039;
        private const int right = 7056;
    
        [Benchmark(Baseline = true)]
        public int Scenario1()
        {
            return int.Parse(left.ToString() + right.ToString());
        }
    
        [Benchmark]
        public int Scenario2()
        {
            return Convert.ToInt32(string.Format("{0}{1}", left, right));
        }
    
        [Benchmark]
        public int Scenario3()
        {
            const int left = 1039;
            const int right = 7056;
    
            int rightLength = 10;
    
            while (rightLength <= right)
            {
                rightLength *= 10;
            }
    
            return (left * rightLength) + right;
        }
    
        /*
         * // * Summary *
         * BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22621
         * 11th Gen Intel Core i7-1165G7 2.80GHz, 1 CPU, 8 logical and 4 physical cores
         * .NET SDK=7.0.401
         *   [Host]     : .NET 6.0.22 (6.0.2223.42425), X64 RyuJIT  [AttachedDebugger]
         *   Job-GHXRTE : .NET 6.0.22 (6.0.2223.42425), X64 RyuJIT
     * 
     * Platform=X64  Runtime=.NET 6.0
     * 
     * |    Method |      Mean |     Error |    StdDev | Ratio | RatioSD |  Gen 0 | Allocated |
     * |---------- |----------:|----------:|----------:|------:|--------:|-------:|----------:|
     * | Scenario1 | 41.319 ns | 0.6891 ns | 0.5754 ns |  1.00 |    0.00 | 0.0166 |     104 B |
     * | Scenario2 | 68.891 ns | 1.2541 ns | 1.2878 ns |  1.66 |    0.03 | 0.0139 |      88 B |
     * | Scenario3 |  1.088 ns | 0.0407 ns | 0.0381 ns |  0.03 |    0.00 |      - |         - |
     * 
     * Run time: 00:01:08 (68.84 sec), executed benchmarks: 3
     * Global total time: 00:01:17 (77.57 sec), executed benchmarks: 3 
     */
}

This is the config, for anyone interested:

public class BenchmarkConfig
{
    /// <summary>
    /// Get a custom configuration
    /// </summary>
    /// <returns></returns>
    public static IConfig Get()
    {
        return ManualConfig.CreateEmpty()
            // Jobs
            .AddJob(Job.Default
                .WithRuntime(CoreRuntime.Core60)
                .WithPlatform(Platform.X64))
            // Configuration of diagnosers and outputs
            .AddDiagnoser(MemoryDiagnoser.Default)
            .AddColumnProvider(DefaultColumnProviders.Instance)
            .AddLogger(ConsoleLogger.Default)
            .AddExporter(CsvExporter.Default)
            .AddExporter(HtmlExporter.Default)
            .AddAnalyser(GetAnalysers().ToArray());
    }

    /// <summary>
    /// Get analyser for the cutom configuration
    /// </summary>
    /// <returns></returns>
    private static IEnumerable<IAnalyser> GetAnalysers()
    {
        yield return EnvironmentAnalyser.Default;
        yield return OutliersAnalyser.Default;
        yield return MinIterationTimeAnalyser.Default;
        yield return MultimodalDistributionAnalyzer.Default;
        yield return RuntimeErrorAnalyser.Default;
        yield return ZeroMeasurementAnalyser.Default;
        yield return BaselineCustomAnalyzer.Default;
    }
}

Upvotes: 0

Kwazi Kubheka
Kwazi Kubheka

Reputation: 1

// Concatenating two numbers program//

        Console.WriteLine("Enter a number for a");
        int a = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a number for b");
        int b = int.Parse(Console.ReadLine());

        int Concatenating = Convert.ToInt32(string.Format("{0}{1}", a, b));
        Console.WriteLine(Concatenating);
        Console.ReadKey();

Upvotes: 0

P_P
P_P

Reputation: 787

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;
}

Upvotes: 22

Cade Roux
Cade Roux

Reputation: 89721

If we want integer result then:

int result = int.Parse(input1.ToString() + input2.ToString());

For a string result do this:

string result = input1.ToString() + input2.ToString();

Upvotes: 3

Skizz
Skizz

Reputation: 71090

I don't think you can get any simpler than this:

static uint Concat (uint a, uint b)
{
  uint
    pow = 1;

  while (pow < b)
  {
    pow = ((pow << 2) + pow) << 1;
    a = ((a << 2) + a) << 1;
  }

  return a + b;
}

which has no memory allocations, string conversions or multiplies; or maybe:

static uint Concat (uint a, uint b)
{
  uint
    pow = 1;

  while (pow < b)
  {
    pow = ((pow << 2) + pow) << 1;
  }

  return a * pow + b;
}

If you want to concatenate two binary numbers:

static uint Concat (uint a, uint b)
{
  uint
    mask = uint.MaxValue;

  while ((mask & b) != 0)
  {
    mask <<= 1;
    a <<= 1;
  }

  return a | b;
}

Upvotes: 12

Tim Hoolihan
Tim Hoolihan

Reputation: 12396

  1. string ConcatInt(int x,int y){return String.Format("{0}{1}",x,y);}
    
  2. int ConcatInt(int x,int y){
       return (x * Math.Pow(10, y.length)) + y;
    }
    

Edit Note: Fixes some mistypes. There are more type issues left. I'm just giving an outline of the answer

The second method should actually be:

static int ConcatInt2(int x, int y) {
   return (int)(x * Math.Pow(10, y.ToString().Length)) + y;
}

Upvotes: 8

Jimmy
Jimmy

Reputation: 91482

how about this?

int c = b;
while(c > 0) {
   a *= 10;
   c /= 10;
}
a += b;

Upvotes: 2

C. Ross
C. Ross

Reputation: 31848

The "Mathy" and "No String" method follows:

    int a = 1039;
    int b = 7056;

    int bLen = (int)Math.Ceiling(Math.Log10(b));
    int ab = (a * ((int)Math.Pow(10, bLen))) + b;

Note that it may still be slow because of the Log10 call.

Upvotes: 2

Jamie Ide
Jamie Ide

Reputation: 49291

public int ConcatInts(int int1, int int2)
{
    return (int)(int1 * Math.Pow(10, int2.ToString().Length)) + int2;
}

Edit: Guess I wasn't the first with this solution!

Upvotes: 1

Rex M
Rex M

Reputation: 144162

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).

Upvotes: 75

jitter
jitter

Reputation: 54615

If you want to concatenate many ints to a String

StringBuilder sb = new StringBuilder(1039);
sb.Append(7056);
sb.Append(1234);
sb.Append(1235);
....
sb.Append(9999);
sb.ToString();

Upvotes: 7

whatsisname
whatsisname

Reputation: 6170

inexpensive? String concatenation or formatted string is probably going to be considerably faster.

Otherwise you can do something like:

Math.Pow(10,Math.Ceiling(Math.Log10(second)))*first+second

provided first and second are integers. This is about the only way you'll do it not involving converting to a string and back, but I am extremely doubtful that it will be faster.

Upvotes: 8

Related Questions