Reputation: 153
I'm trying to convert the string values to integers, however whenever i do this, it gives me the ASCI value of the character instead of the actual typed string.
Example:
a user inputs "11101" on there keyboard to a string variable named binary
for(int i = 0; i < binary.length(); i++){
storage[i] = Integer.valueOf(binary.charAt(i));
}
if I look at the values of storage[i], i will see only the ASCI values of the numbers, and not the actual 1's or 0's
Upvotes: 0
Views: 160
Reputation: 11704
for(int i = 0; i < binary.length(); i++){
storage[i] = binary.charAt(i) - '0';
}
Upvotes: 0
Reputation: 94653
Use Integer.parseInt(str,radix)
method.
int no=Integer.parseInt("11101",2);
//or
int no=Integer.parseInt("11101");
Upvotes: 3
Reputation: 1045
Try this:
for(int i = 0; i < binary.length(); i++){
storage[i] = Integer.valueOf(binary.substring(i, i + 1));
}
Upvotes: 2
Reputation: 3120
for(int i = 0; i < binary.length(); i++){
storage[i] = Integer.parseInt(binary.charAt(i), 2);
}
Upvotes: 2
Reputation: 4017
This should do the trick.
storage[i] = Integer.parseInt( binary.charAt(i));
Upvotes: 2