It is possible to concatenate numbers of type int

Asked

Viewed 4,835 times

7

For example 10 and 12, concatenated would be 1012, this without being string, it would have to be the whole kind, you can do it?

  • Of course. By string is a more lazy solution, it works well

3 answers

13


There is, but the algorithm that works in all cases would be complex, almost always pays more to use the string to make the concatenation.

So if you’re expecting an instruction or a ready-made function in the language that delivers the result, you don’t have.

The mathematical basis that you should use to build the algorithm is to multiply the first number by 10 to the number of digits of it, in example 10 has 2 digits, so you should multiply by 100, then just add with the second.

In any numerical basis each digit on the left has a higher weight in geometric progression according to its. So whenever you want to put digits to the left you should raise the base (in case 10 since we deal with decimal) to the position of the digit then 1234 + 123 + 12 You sail like this:

1 x 108 + 2 x 107 + 3 x 106 + 4 x 105 + 1 x 104 + 2 x 103 + 3 x 102 + 1 x 101 + 2 x 100

1 x 100,000,000 + 2 x 10,000,000 + 3 x 1,000,000 + 4 x 100,000 + 1 x 10.000 + 2 x 1.000 + 3 x 100 + 1 x 10 + 1 x 2

100.000.000 + 20.000.000 + 3.000.000 + 400.000 + 10.000 + 2.000 + 300 + 10 + 2

123.412.312

Obviously you have to do the reverse process to decompose the number into digits.

If you have any specific programming questions while doing it you can ask a specific question.

6

For a general solution, see the @Maniero response. If you really need this right, it compensates for a specific function of yours.

for int output, see at the end of the answer

Generating a string:

If it’s for something fast, like a debug, or just an output formatting, C has an alternative which is the family of printf and his pair sprintf for storage.

Example

Playing in the stdout:

printf("%02d%02d\n", v1, v2);

See working on IDEONE.


To write to a buffer:

sprintf( destino, "%02d%02d", v1, v2);


Basically these functions receive a string interpolated, and the parameters to interpolate. In our case:

┌──── % marca a posição onde entrará um valor interpolado
│┌─── significa "preencher com zero a esquerda
││┌── indica que o preenchimento deve ter 2 casas
│││┌─ indica que o valor interpolado será tratado como decimal
%02d

Here is more interesting information:

What are the differences between printf, fprintf, sprintf, snprintf, printf_s and fprintf_s?

Generating a int:

If you need numerical values, concatenation is mathematical:

concatenado = valor1 * 100 + valor2;

Or, limiting to two houses discarding overflow:

concatenado = ( valor1 % 100 ) * 100 + ( valor2 % 100 )

(which is already in @Maniero’s reply, I just applied to your case).

2

Guys, I got my ass kicked, but I found a sweet little solution. I needed to concatenate three bytes that form a hexa number of 6 digits and is used to identify a device.

Look at the solution. I moved 16 bits to the left in the first number, 8 in the second and I disappeared with the third.

unsigned char v1 = 0x0B; // 11 unsigned char v2 = 0xF8; // 248 unsigned char v3 = 0xE9; // 233

unsigned int r = (v1 << 16) + (v2 << 8) + (v3);

printf("Value: %x n", r);

Pay attention to the unsigned "char" because the flagged the first bit is 1 to positive, which changes the result. If you want to flag, it is better to use bit fields separating the flag.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.