## digitalmars.D.learn - Concatenates int

• Sean Campbell (2/2) Jul 10 2014 i have the ints 4, 7, 0 and 1 how can i Concatenate them into
• Rikki Cattermole (9/11) Jul 10 2014 If we talking at compile time definition:
• bearophile (8/12) Jul 10 2014 And to concatenate them there is "join" (joiner is not yet usable
• simendsjo (3/21) Jul 10 2014 D also has the pow operator, so you can write this as:
"Sean Campbell" <sycam.inc gmail.com> writes:
```i have the ints 4, 7, 0 and 1 how can i Concatenate them into
four thousand seven hundred and one.
```
Jul 10 2014
Rikki Cattermole <alphaglosined gmail.com> writes:
```On 11/07/2014 12:11 a.m., Sean Campbell wrote:
i have the ints 4, 7, 0 and 1 how can i Concatenate them into four
thousand seven hundred and one.

If we talking at compile time definition:

int myint = 4_7_0_1;

Would work.
However I'll assume its at runtime you really want this.

I.e. converting a string to an integer.

int myint = to!int("4" ~ "7" ~ "0" ~ "1");

Now they are not strings, and the positions of 10^ doesn't change then:

int myint = (1000 * 4) + (100 * 7) + 1;
```
Jul 10 2014
"bearophile" <bearophileHUGS lycos.com> writes:
```Rikki Cattermole:

int myint = to!int("4" ~ "7" ~ "0" ~ "1");

And to concatenate them there is "join" (joiner is not yet usable
here, because to!() doesn't yet accept a lazy input,
unfortunately).

Now they are not strings, and the positions of 10^ doesn't
change then:

int myint = (1000 * 4) + (100 * 7) + 1;

Even if Phobos doesn't yet have a enumerate() function, you can
use a iota+zip+reduce to do this.

Bye,
bearophile
```
Jul 10 2014
simendsjo <simendsjo gmail.com> writes:
```On 07/10/2014 02:22 PM, Rikki Cattermole wrote:
On 11/07/2014 12:11 a.m., Sean Campbell wrote:
i have the ints 4, 7, 0 and 1 how can i Concatenate them into four
thousand seven hundred and one.

If we talking at compile time definition:

int myint = 4_7_0_1;

Would work.
However I'll assume its at runtime you really want this.

I.e. converting a string to an integer.

int myint = to!int("4" ~ "7" ~ "0" ~ "1");

Now they are not strings, and the positions of 10^ doesn't change then:

int myint = (1000 * 4) + (100 * 7) + 1;

D also has the pow operator, so you can write this as:

int i = 4*10^^3 + 7*10^^2 + 0*10^^1 + 1*10^^0;
```
Jul 10 2014
"Sean Campbell" <sycam.inc gmail.com> writes:
```perhaps I'd better state what I'm doing.
i have an array of 4 bytes and a want to convert them to a 32 bit
int
and convert the 32 bit int back into a 4 bytes again.
```
Jul 10 2014
Rikki Cattermole <alphaglosined gmail.com> writes:
```On 11/07/2014 1:18 a.m., Sean Campbell wrote:
perhaps I'd better state what I'm doing.
i have an array of 4 bytes and a want to convert them to a 32 bit
int
and convert the 32 bit int back into a 4 bytes again.

Small hack I use in Dakka:

union RawConvTypes(T) {
T value;
ubyte[T.sizeof] bytes;

ubyte[T.sizeof] opCast() {
return bytes;
}
}

auto iRCT = RawConvTypes!int(5);
assert(iRCT.bytes == [5, 0, 0, 0]);

Can be quite useful for evil conversions.
```
Jul 10 2014
"Sean Campbell" <sycam.inc gmail.com> writes:
```On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote:
On 11/07/2014 1:18 a.m., Sean Campbell wrote:
perhaps I'd better state what I'm doing.
i have an array of 4 bytes and a want to convert them to a 32
bit
int
and convert the 32 bit int back into a 4 bytes again.

Small hack I use in Dakka:

union RawConvTypes(T) {
T value;
ubyte[T.sizeof] bytes;

ubyte[T.sizeof] opCast() {
return bytes;
}
}

auto iRCT = RawConvTypes!int(5);
assert(iRCT.bytes == [5, 0, 0, 0]);

Can be quite useful for evil conversions.

this may sound stupid (new to system programming) but how do you
convert to int form ubyte[]
```
Jul 10 2014
"Olivier Pisano" <olivier.pisano laposte.net> writes:
```Hello,

I may have not understood what you actually want to do, but
aren't std.bitmanip.peek or std.bitmanip.read what you are
looking for ?

http://dlang.org/phobos/std_bitmanip.html#.peek
```
Jul 10 2014
"Sean Campbell" <sycam.inc gmail.com> writes:
```On Thursday, 10 July 2014 at 15:51:11 UTC, Olivier Pisano wrote:
Hello,

I may have not understood what you actually want to do, but
aren't std.bitmanip.peek or std.bitmanip.read what you are
looking for ?

http://dlang.org/phobos/std_bitmanip.html#.peek

std.bitmanip.peek and std.bitmanip.read will do nicely should of
looked more closely
if I need to Concatenate ints I'l just use a recursive pow based
on length

int ConcatInt(int[] anint){
int total = 0;
for(int i=0;i<anint.length;i++){
total += anint[i]*10^^(anint.length-i-1);
}
}
```
Jul 10 2014
"sigod" <sigod.mail gmail.com> writes:
```On Thursday, 10 July 2014 at 17:30:17 UTC, Sean Campbell wrote:
if I need to Concatenate ints I'l just use a recursive pow
based on length

int ConcatInt(int[] anint){
int total = 0;
for(int i=0;i<anint.length;i++){
total += anint[i]*10^^(anint.length-i-1);
}
}

With `foreach_reverse` it looks a little better:

```d
int concat_ints(int[] ints)
{
int result;

foreach_reverse (i, int_; ints) {
result += int_ * 10 ^^ (ints.length - i - 1);
}

return result;
}
```
```
Jul 10 2014
```On Thursday, 10 July 2014 at 15:14:21 UTC, Sean Campbell wrote:
On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole
wrote:
On 11/07/2014 1:18 a.m., Sean Campbell wrote:
perhaps I'd better state what I'm doing.
i have an array of 4 bytes and a want to convert them to a 32
bit
int
and convert the 32 bit int back into a 4 bytes again.

Small hack I use in Dakka:

union RawConvTypes(T) {
T value;
ubyte[T.sizeof] bytes;

ubyte[T.sizeof] opCast() {
return bytes;
}
}

auto iRCT = RawConvTypes!int(5);
assert(iRCT.bytes == [5, 0, 0, 0]);

Can be quite useful for evil conversions.

this may sound stupid (new to system programming) but how do
you convert to int form ubyte[]

int to ubyte[4]:
auto iRCT = RawConvTypes!int();
iRCT.value = 5;
writeln(iRCT.bytes); // [5, 0, 0, 0]

ubyte[4] to int:
auto iRCT = RawConvTypes!int();
iRCT.bytes = [0, 1, 0, 0];
writeln(iRCT.value); // 256
```
Jul 10 2014
"Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
```On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote:
On 11/07/2014 1:18 a.m., Sean Campbell wrote:
perhaps I'd better state what I'm doing.
i have an array of 4 bytes and a want to convert them to a 32
bit
int
and convert the 32 bit int back into a 4 bytes again.

Small hack I use in Dakka:

union RawConvTypes(T) {
T value;
ubyte[T.sizeof] bytes;

ubyte[T.sizeof] opCast() {
return bytes;
}
}

auto iRCT = RawConvTypes!int(5);
assert(iRCT.bytes == [5, 0, 0, 0]);

Can be quite useful for evil conversions.

But as I understood the OP, he want's to use the bytes as decimal
digits, i.e.

assert(my_convert([4,7,0,1]) == 4701);

Reinterpret casting will not do this...
```
Jul 10 2014