1
string test; test="3527523"; int a=test[3]; std::cout<<a<<std::endl; 

I was expecting output to be number 7, but I got 55 instead. I have searched through different posts, they all used this method without any problems.. I just don't know where the problem is in my case. Thank you for your help!

3
  • 1
    You could try this: atoi Commented May 11, 2013 at 0:14
  • 6
    the value of test[3] is '7' not 7. the ascii character '7' evaluates to 55. see cpptutor.com/imgs/ascii_table.gif Commented May 11, 2013 at 0:15
  • 1
    Instead of int a = test[3] try this: int a = test[3] - '0'. That should print 7. Commented May 11, 2013 at 0:17

5 Answers 5

6

test[3] returns char, which you're converting to an integer, so it returns the ASCII value of the character. 3 in ASCII is 55.

Try using char instead as the type of a:

char a = test[3]; std::cout << a; // 3 

If you wanted the result to be an integer, subtract the character from '0':

std::string test = "3527523"; int a = test[3] - '0'; std::cout << a; // 3 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot for the help. I know I asked a really stupid question So what does '0' really do in here to make it a correct ans?
user2341380, If you consult a table of ASCII values, the values for '0', '1', '2', '3', '4'... are 48, 49, 50, 51... so when you subtract 51 ('3') from 48 ('0') you get 3. Works for all ASCII digits.
'0' is a character literal and has the ASCII value of 48. When arithmetic is applied to at least one operand of type char, integral promotion is applied. That means '0' is converted to 48. The values of the characters from '0' to '9' are 48 to 53 (do you get it now) :)?
@user2341380 Let's say we wanted to convert the character '1' to an integer. '1' has the ASCII value 49. Subtracting from '0' (48) gives the integer 1 (49 - 48 == 0).
@user2341380 Sorry, I meant (49 - 48 == 1).
0

You get the char '7' by the brackets, which has the ASCII code 55 (48+7).

Comments

0

Problem is that you assign test[3] to a variable of type int, thus causing the actual character code (55) to be printed instead of the represented character ('7'). Replace int with char or static_cast before printing.

Comments

0

You don't "cast", you "convert".

You can use sscanf(), atoi() among other functions to convert the value of a string:

int iret = sscanf (test, "%d", &a);

The value of test[3] is the ASCII character '7', which is actually the integer "55".

PS: The beauty of "sscanf()" is that you can easily detect if a parse error occurred by checking for "iret != 1".

Comments

0

Use atoi:

int atoi (const char * str); //Convert string to integer

For example:

string test; test="3527523"; const char c = test[3]; int a=atoi(&c); std::cout<<a<<std::endl; 

2 Comments

i will then be equal to the number 3527523. That's not what he wanted.
Requires a C-string. Plus, that won't get the value of one digit, as the OP's code shows an attempt to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.