Solved

CONVERT char in int C++

Posted on 2010-09-15
6
461 Views
Last Modified: 2013-12-14
char vector[250];
int x;

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

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

Open in new window



Any suggestions?
0
Comment
Question by:CreuLINUX
  • 2
  • 2
  • 2
6 Comments
 
LVL 32

Expert Comment

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

Accepted Solution

by:
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

by:phoffric
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:CreuLINUX
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

by:Daniel Junges
ID: 33685315
convert you vector to a string then:

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

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now