How to convert from string to char in C++?

String to Integer Java question?

  • I have to take a string, like "2112211241" or something and convert it into an integer array, but have no idea how. I wanted to do for(int j = 0; j < i; j++) { for(int q = 0; q < 10; q++) { char temp = studentAnswers[j].charAt(q); studentIntAnswers[j][q] = Integer.parseInt(temp); // earlier defined integer array. } } But there isn't a method for parseInt(char). Is there an easier way to do that? Thanks!

  • Answer:

    You're right: there isn't a method to parse a char into an int but there is a method to parse a string into an in. So, just represent each char as a single char string. This is how you do it: String str = "2112211241"; int[] digit = new int[str.length()]; for( int k=0;k<str.length();k++ ) { ...digit[k] = Integer.parseInt( str.substring(k,k+1) ); } Don't forget to give me my "best answer" rating!!!

Hayden at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

keep it a string, basically, char temp = charAt part should be int temp = Integer.parseInt(studentAnswers[j].subst… EDIT: don't know why it's making it say ... but substring(q,q+1));

David

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.