Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert char to int

34 views
Skip to first unread message

Joseph Hesse

unread,
Aug 23, 2016, 1:06:15 PM8/23/16
to
I have a char x = '8' and want to use this to put the integer 8 into an
int variable. I know I can do it this way:

char x = '8';
int y = x -'0';

Is this the C++ 11 way of doing this or is there a better way? Should I
put x into a string of length 1 and use atoi?

Thank you,
Joe

Victor Bazarov

unread,
Aug 23, 2016, 1:23:38 PM8/23/16
to
There is no need for atoi. The C++ character set works the way you
showed for all decimal digits.

V
--
I do not respond to top-posted replies, please don't ask

Scott Lurndal

unread,
Aug 23, 2016, 3:28:43 PM8/23/16
to
Victor Bazarov <v.ba...@comcast.invalid> writes:
>On 8/23/2016 1:05 PM, Joseph Hesse wrote:
>> I have a char x = '8' and want to use this to put the integer 8 into an
>> int variable. I know I can do it this way:
>>
>> char x = '8';
>> int y = x -'0';
>>
>> Is this the C++ 11 way of doing this or is there a better way? Should I
>> put x into a string of length 1 and use atoi?
>
>There is no need for atoi. The C++ character set works the way you
>showed for all decimal digits.

C++ character set?

In reality, whether the underlying encoding is ASCII, EBCDIC or UTF-8,
the technique of subtracting the character '0' from any other numeric
character value will work properly.

Victor Bazarov

unread,
Aug 23, 2016, 3:39:02 PM8/23/16
to
On 8/23/2016 3:28 PM, Scott Lurndal wrote:
> Victor Bazarov <v.ba...@comcast.invalid> writes:
>> On 8/23/2016 1:05 PM, Joseph Hesse wrote:
>>> I have a char x = '8' and want to use this to put the integer 8 into an
>>> int variable. I know I can do it this way:
>>>
>>> char x = '8';
>>> int y = x -'0';
>>>
>>> Is this the C++ 11 way of doing this or is there a better way? Should I
>>> put x into a string of length 1 and use atoi?
>>
>> There is no need for atoi. The C++ character set works the way you
>> showed for all decimal digits.
>
> C++ character set?
> [.. blahblah ..]

Yes, please see the Standard, [lex.charset]. The first paragraph starts
with "The /basic character set/ consists".

David Brown

unread,
Aug 24, 2016, 3:06:10 AM8/24/16
to
Yes, but if you read what it says, it only guarantees that the relevant
characters '0', '1', etc., are available. It does /not/ guarantee that
their codes are in ascending order.

It just so happens that in /practical/ character sets, which will be
either ASCII (at least for the 0..0x7f range - including UTF-8) or
EBCDIC, the digits are in that convenient order.

So as long as you are only using the code on one system, or are happy
with "portable to almost anything, but not guaranteed by the standards",
then it's fine to convert single digit characters to integers in this way.

It might be worth putting in a range check, of course.


Barry Schwarz

unread,
Aug 24, 2016, 4:43:17 AM8/24/16
to
On Wed, 24 Aug 2016 09:05:52 +0200, David Brown
<david...@hesbynett.no> wrote:

>On 23/08/16 21:38, Victor Bazarov wrote:
>> On 8/23/2016 3:28 PM, Scott Lurndal wrote:
>>> Victor Bazarov <v.ba...@comcast.invalid> writes:
>>>> On 8/23/2016 1:05 PM, Joseph Hesse wrote:
>>>>> I have a char x = '8' and want to use this to put the integer 8 into an
>>>>> int variable. I know I can do it this way:
>>>>>
>>>>> char x = '8';
>>>>> int y = x -'0';
>>>>>
>>>>> Is this the C++ 11 way of doing this or is there a better way? Should I
>>>>> put x into a string of length 1 and use atoi?
>>>>
>>>> There is no need for atoi. The C++ character set works the way you
>>>> showed for all decimal digits.
>>>
>>> C++ character set?
>>> [.. blahblah ..]
>>
>> Yes, please see the Standard, [lex.charset]. The first paragraph starts
>> with "The /basic character set/ consists".
>>
>
>Yes, but if you read what it says, it only guarantees that the relevant
>characters '0', '1', etc., are available. It does /not/ guarantee that
>their codes are in ascending order.

Yes it does. Two paragraphs later it states "In both the source and
execution basic character sets, the value of each character after 0 in
the above list of decimal digits shall be one greater than the value
of the previous."

--
Remove del for email

David Brown

unread,
Aug 24, 2016, 7:33:46 AM8/24/16
to
You are right - I missed that. Sorry for my mistake here, and thanks
for the correction.

0 new messages