?
Solved

CONVERT char in int C++

Posted on 2010-09-15
6
Medium Priority
?
473 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
[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
  • Learn & ask questions
  • 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

777 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