Solved

CONVERT char in int C++

Posted on 2010-09-15
472 Views
``````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
Question by:CreuLINUX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2

LVL 32

Expert Comment

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

LVL 14

Accepted Solution

Daniel Junges earned 500 total points
ID: 33684471
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

LVL 32

Expert Comment

ID: 33684500
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 Comment

ID: 33685128
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

LVL 14

Expert Comment

ID: 33685315
convert you vector to a string then:

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

Author Comment

ID: 33685665
but in c++ not c#
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month6 days, 3 hours left to enroll