x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 480

# CONVERT char in int C++

``````char vector[250];
int x;

cout <<  "entry type:  ";
cin >> vector;

# example vector = {'0','1','0','1','1' };
x = vector[0];
# example x = 0 (interger)
``````

Any suggestions?
0
CreuLINUX
• 2
• 2
• 2
1 Solution

Commented:
Not sure what the question is. What problem are you having?
0

Commented:
try this:

x = (int)vector[0]; // here you get the ascii code

x = (int)vector[0] - 48; // here you get the number "only for numerics"
0

Commented:
Well, if you are trying to convert an ascii representation of a digit, then you can say:
x = vector[0] - '0';  // Using '0' is preferable to magic numbers like 48
Converting a char to an int does not require a type cast, although it doesn't hurt to have it.
0

Author Commented:
drawing on the question whether I have

[code]
char vector[250];
int result;
#example vector = {'0','1','0','1','1' };
# in binary this would be: 01011
# in decimal: 11
# how do I get the "result" to receive the value 11?
result = 11;
[/code]
0

Commented:
convert you vector to a string then:

int value = Integer.parseInt("1001", 2);**//*2*for*binary
0

Author Commented:
but in c++ not c#
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.