Re: [Dev-C++] how do you convert char to int?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2001-12-09 23:08:50
|
A couple of notes, atoi() works on a string, so you would have to ensure
that a terminator after the char is there. It doesn't have to be a null
though, because atoi() stops when it reaches a non-numerical character, i.e.
whitespace, letter, etc... Another way of doing it is:
static_cast<int>(MyInt -'0');
which relies on the ascii values of a character and the fact that 0 through
9 are in order. You would have to check that the values are actualy number
characters before using them of course. Also this only works on a single
character and not a value that is larger than 9, although it can be used as
the basis of creating your own atoi() style function.
Jason.
----- Original Message -----
From: "Kevin Wood" <ke...@me...>
To: <dev...@li...>
Sent: Sunday, December 09, 2001 10:09 PM
Subject: Re: [Dev-C++] how do you convert char to int?
> I would use
>
> #include <stdlib.h>
> int MyInt=atoi('7');
>
> Kev.
>
>
> ----- Original Message -----
> From: chromatonus 712 <chr...@ho...>
> To: <dev...@li...>
> Sent: Sunday, December 09, 2001 8:56 PM
> Subject: [Dev-C++] how do you convert char to int?
>
>
> > so if you enter 7 as char, for example, how do you convert it to the
> number
> > 7?
> >
> > _________________________________________________________________
> > Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp
> >
> >
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|