Converting char to int

thedeemon dlang at thedeemon.com
Tue Dec 31 23:10:37 PST 2013


On Wednesday, 1 January 2014 at 06:21:05 UTC, Caeadas wrote:
> My issue is this: I'm trying to convert character types to
> integers using to!int from std.conv, and I'm getting, for
> example, '0'->48, '1'->49, etc. It seems like there should be a
> simple way around this, but it's eluding me.

Well, in ASCII (as well as UTF-8 and many others) the character 
'0' is represented by number 48, character '1' is 49 etc., so if 
you want to convert '5' to 5 you just need to subtract '0's 
number from it, e.g.
char c = ...;
int x = c - '0';


More information about the Digitalmars-d-learn mailing list