Multi-Char array to int function
in
Programming Questions
•
10 months ago
So i am designing code to convert a string such as ("AA3") to and integer and vice versa. The integer is the result of a nested loop set up that i have been trying to get to work.
- char numbers[] = {
- '0','1','2','3','4','5','6','7','8','9'};
- char letters[] = {
- 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
- char test[] = {'B','G','6'};
- char[] output;
- int outputf; //first output
- int outputs; // second output
- int outputt; // third output
- void setup() {
- convertID(test);// testing the main function.
- }
- char[] convertID(int id) {
- char[] output = {'A','A','0'};
- return output;
- }
- int convertID(char[] chid) {
- int outputf = 0;
- int outputs = 0;
- int outputt = 0;
- for(int i = 0;i < letters.length;i++) {// first loop checks what character the first space is and starts filling the integer.
- if (chid[0] == letters[i]) {
- outputf = 270 * (int)letters[i];
- }
- for (int h = 0;h < letters.length;h++) { // second loop checks the second character and applys it to the int.
- if(chid[1] == letters[h]) {
- outputs = 10 * (int)letters[h];
- }
- for (int a = 0; i < numbers.length - 1;a++) { //third loop checks the number at the end for its value.
- if(chid[2] == numbers[a]) {
- outputt = 1 * numbers[a];
- }
- }
- }
- }
- return outputf + outputs + outputt; //returns the sum of all three spaces.
- }
The problem ive been encountering is that on line "if(chid[2] == numbers[a]) {" ive been getting an array out of bounds exception which means i did something wrong with the arrays.
If someone could just help me to first get rid of this error and secondly get this program to work for its intended purpose.
Thank you all in advance.
1