Problem
Quite a common task in programming is the need to find the ASCII value of a character. The code below demonstrates how to do this in D.
Solution
int main(char[][] args) {
auto ch = 'A';
int charAsInt = ch;
printf( "%d", charAsInt );
return 0;
}
In D, getting the ASCII value of a character a trivial task. Simply assign the character to an integer and D will “promote” the character to reveal its ASCII value.
Some other points to note about the above code are:
- char values in D are UTF-8 by default; if you want UTF-16 or UTF-32 characters then use the wchar or dchar types respectively.
- Also note the use of the auto keyword when I declared the character variable, ch. The auto keyword gives you access to D implicit type inference features.