convert between types

Steven Schveighoffer schveiguy at gmail.com
Thu Jan 8 19:44:10 UTC 2026


On Friday, 2 January 2026 at 15:30:06 UTC, Lars Johansson wrote:
> On Friday, 2 January 2026 at 15:02:09 UTC, Kapendev wrote:
>> On Friday, 2 January 2026 at 13:09:44 UTC, Lars Johansson 
>> wrote:
>>>     I'm writing a simple program just to get started (se 
>>> below).
>>>      mat[i][z] = z; this line generate following error:
>>>      cannot implicitly convert expression `z` of type `ulong` 
>>> to `char`
>>
>> Simple as:
>>
>> ```d
>> mat[i][z] = cast(char) z;
>> ```
>
> Hi thank you for your reply.
> But that gives me a blank result (white space). I have to add 
> '0' to get the 'source' value from the cast.

```d
mat[i][z] = "0123456789"[z];
```

Other than that, your solution is probably the best. Another 
thing you can do is instead of casting, use `(z + '0') & 0x7f`.

The above has the benefit that if you exceed the "expected" 
limit, you will get an out of bounds error instead of garbage 
characters.

-Steve


More information about the Digitalmars-d-learn mailing list