Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 10, 2014 Concatenates int | ||||
---|---|---|---|---|
| ||||
i have the ints 4, 7, 0 and 1 how can i Concatenate them into four thousand seven hundred and one. |
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Campbell | 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;
|
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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 |
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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;
|
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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. |
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Campbell | 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.
|
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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[]
|
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Campbell | 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 |
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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...
|
July 10, 2014 Re: Concatenates int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Campbell | 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
|
Copyright © 1999-2021 by the D Language Foundation