Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 25, 2017 Problem while copy Array Element to variable | ||||
---|---|---|---|---|
| ||||
My Dynamic array completely work good but when assign it's one index to a variable, it's saves ASCII of that index. writeln(myarray); // output 24 var = myarray[0]; // it assign 50 to var Why changed to ASCII and how to get rid of. |
July 25, 2017 Re: Problem while copy Array Element to variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zaheer Ahmed | On 07/25/2017 09:14 AM, Zaheer Ahmed wrote: > My Dynamic array completely work good but when assign it's one index to > a variable, it's saves ASCII of that index. > writeln(myarray); // output 24 > var = myarray[0]; // it assign 50 to var > Why changed to ASCII and how to get rid of. First, just to remind you that this question would be more helpful on the Learn forum. :) You're not showing any code so I try to reproduce the issue: import std.stdio; void main() { auto myarray = "24"; writeln(myarray); // output 24 int var; auto var = myarray[0]; // it assign 50 to var writeln(var); } myarray[0] has the value 50 because 50 is the ASCII code of '2'. The reason var gets 50 in the above scenario is because var is an int. If you want to get char '2' (of course still with the value 50), you can make it char: char var; On the other hand, if you want to get the integer value 2, there is the trick of subtracting the value of '0' from it: var = myarray[0] - '0'; var now has value 2. Ali |
July 25, 2017 Re: Problem while copy Array Element to variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 07/25/2017 10:11 AM, Ali Çehreli wrote:
> int var;
> auto var = myarray[0]; // it assign 50 to var
Remove that 'auto' please. Apparently, I edited my message instead of code. :p
Ali
|
July 25, 2017 Re: Problem while copy Array Element to variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 25 July 2017 at 17:11:37 UTC, Ali Çehreli wrote:
> On 07/25/2017 09:14 AM, Zaheer Ahmed wrote:
> > My Dynamic array completely work good but when assign it's
> one index to
> > a variable, it's saves ASCII of that index.
> > writeln(myarray); // output 24
> > var = myarray[0]; // it assign 50 to var
> > Why changed to ASCII and how to get rid of.
>
> First, just to remind you that this question would be more helpful on the Learn forum. :)
>
> You're not showing any code so I try to reproduce the issue:
>
> import std.stdio;
>
> void main() {
> auto myarray = "24";
> writeln(myarray); // output 24
> int var;
> auto var = myarray[0]; // it assign 50 to var
> writeln(var);
> }
>
> myarray[0] has the value 50 because 50 is the ASCII code of '2'.
>
> The reason var gets 50 in the above scenario is because var is an int. If you want to get char '2' (of course still with the value 50), you can make it char:
>
> char var;
>
> On the other hand, if you want to get the integer value 2, there is the trick of subtracting the value of '0' from it:
>
> var = myarray[0] - '0';
>
> var now has value 2.
>
> Ali
I got this with var = myarray[0] - '0'; without this I have to manually pollute my code.
Thanks for Solution & also for writing Book on D Lang.
|
Copyright © 1999-2021 by the D Language Foundation