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 (4/4) Jul 10 2014 perhaps I'd better state what I'm doing.
- Rikki Cattermole (12/16) Jul 10 2014 Small hack I use in Dakka:
- Sean Campbell (3/20) Jul 10 2014 this may sound stupid (new to system programming) but how do you
- Olivier Pisano (5/5) Jul 10 2014 Hello,
- Sean Campbell (12/17) Jul 10 2014 std.bitmanip.peek and std.bitmanip.read will do nicely should of
- sigod (12/21) Jul 10 2014 With `foreach_reverse` it looks a little better:
- Yota (9/35) Jul 10 2014 int to ubyte[4]:
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (5/22) Jul 10 2014 But as I understood the OP, he want's to use the bytes as decimal
i have the ints 4, 7, 0 and 1 how can i Concatenate them into four thousand seven hundred and one.
Jul 10 2014
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
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
On 07/10/2014 02:22 PM, Rikki Cattermole wrote:On 11/07/2014 12:11 a.m., Sean Campbell wrote: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;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
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
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
On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote:On 11/07/2014 1:18 a.m., Sean Campbell wrote:this may sound stupid (new to system programming) but how do you convert to int form ubyte[]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
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 ?
Jul 10 2014
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 ?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); } return total; }
Jul 10 2014
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); } return total; }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: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); // 256On 11/07/2014 1:18 a.m., Sean Campbell wrote:this may sound stupid (new to system programming) but how do you convert to int form ubyte[]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
On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote:On 11/07/2014 1:18 a.m., Sean Campbell wrote: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...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