Go Down

Topic: Converting characters to letter (ascii) string (Read 65 times) previous topic - next topic

mfpx

Hey,
So before anyone asks, this IS a university assignment. Also, I know a lot of things aren't the "best practice" in my code, but I've only been using it for ~1.5 months, and I don't really need that right now.

So, we've got that cleared, now onto the main issue.

In function void aceDecode I'm trying to convert each character or characters from the Serial Monitor input into it's ASCII equivalent.

So what I want is if I type: !++_!_!!&_!!&_&+ +!&_&+_!&+_!!&_!+ (hello world), I expect it to convert to "hello world", however, it appears to be broken. It converts individual characters fine, but converting the whole input just prints the first character a bunch.

Code:
Code: [Select]

//BEGIN DEFINITION BLOCK
//#define DEBUG //MUST be commented for release version
String ace_a = "+";
String ace_b = "!!+";
String ace_c = "!+&";
String ace_d = "!+";
String ace_e = "!";
String ace_f = "!+!";
String ace_g = "++";
String ace_h = "!++";
String ace_i = "!!";
String ace_j = "+!!";
String ace_k = "!&";
String ace_l = "!!&";
String ace_m = "+!";
String ace_n = "+&";
String ace_o = "&+";
String ace_p = "&!";
String ace_q = "+++";
String ace_r = "!&+";
String ace_s = "++!";
String ace_t = "&";
String ace_u = "&&";
String ace_v = "!&!";
String ace_w = "+!&";
String ace_x = "+&!";
String ace_y = "!&&";
String ace_z = "++&";
String ace_1 = "&!!+";
String ace_2 = "&!!&";
String ace_3 = "&!+!";
String ace_4 = "&!++";
String ace_5 = "&!+&";
String ace_6 = "&!&!";
String ace_7 = "&!&+";
String ace_8 = "&!&&";
String ace_9 = "&+!!";
String ace_0 = "&!!!";
String ace_period = "&!!";
String ace_comma = "&!+";
String ace_apostrophe = "&+!";
String ace_question = "&++";
String ace_exclamation = "&+&";
String ace_plus = "&&+";
String ace_minus = "&&!";
String ace_multiply = "&&&";
String ace_divide = "&!&";
String ace_equals = "&+++&";
String ace_space = " ";
String ace_no_match = "NO MATCH";
//END DEFINITION BLOCK

String char2Ace(char a) { //Encode to ACE
  Serial.println(a);
  switch (a) { //switch used for converting letters (ascii) to ACE
    case 'a':
      return ace_a;

    case 'b':
      return ace_b;

    case 'c':
      return ace_c;

    case 'd':
      return ace_d;

    case 'e':
      return ace_e;

    case 'f':
      return ace_f;

    case 'g':
      return ace_g;

    case 'h':
      return ace_h;

    case 'i':
      return ace_i;

    case 'j':
      return ace_j;

    case 'k':
      return ace_k;

    case 'l':
      return ace_l;

    case 'm':
      return ace_m;

    case 'n':
      return ace_n;

    case 'o':
      return ace_o;

    case 'p':
      return ace_p;

    case 'q':
      return ace_q;

    case 'r':
      return ace_r;

    case 's':
      return ace_s;

    case 't':
      return ace_t;

    case 'u':
      return ace_u;

    case 'v':
      return ace_v;

    case 'w':
      return ace_q;

    case 'x':
      return ace_x;

    case 'y':
      return ace_y;

    case 'z':
      return ace_z;

    case '1':
      return ace_1;

    case '2':
      return ace_2;

    case '3':
      return ace_3;

    case '4':
      return ace_4;

    case '5':
      return ace_5;

    case '6':
      return ace_6;

    case '7':
      return ace_7;

    case '8':
      return ace_8;

    case '9':
      return ace_9;

    case '0':
      return ace_0;

    case '.':
      return ace_period;

    case ',':
      return ace_comma;

    case '\'': //backslash is needed to escape the character (DO NOT DELETE!)
      return ace_apostrophe;

    case '?':
      return ace_question;

    case '!':
      return ace_exclamation;

    case '+':
      return ace_plus;

    case '-':
      return ace_minus;

    case '*':
      return ace_multiply;

    case '/':
      return ace_divide;

    case '=':
      return ace_equals;

    case ' ':
      return ace_space;

    default:
      return ace_no_match;
  }
}

char aceDecode2Char(String a) { //an additional step needed to decode to string
  Serial.println(a);
  if (a.equals(ace_a)) {
    return 'a';
  } else if (a.equals(ace_b)) {
    return 'b';
  } else if (a.equals(ace_c)) {
    return 'c';
  } else if (a.equals(ace_d)) {
    return 'd';
  } else if (a.equals(ace_e)) {
    return 'e';
  } else if (a.equals(ace_f)) {
    return 'f';
  } else if (a.equals(ace_g)) {
    return 'g';
  } else if (a.equals(ace_h)) {
    return 'h';
  } else if (a.equals(ace_i)) {
    return 'i';
  } else if (a.equals(ace_j)) {
    return 'j';
  } else if (a.equals(ace_k)) {
    return 'k';
  } else if (a.equals(ace_l)) {
    return 'l';
  } else if (a.equals(ace_m)) {
    return 'm';
  } else if (a.equals(ace_n)) {
    return 'n';
  } else if (a.equals(ace_o)) {
    return 'o';
  } else if (a.equals(ace_p)) {
    return 'p';
  } else if (a.equals(ace_q)) {
    return 'q';
  } else if (a.equals(ace_r)) {
    return 'r';
  } else if (a.equals(ace_s)) {
    return 's';
  } else if (a.equals(ace_t)) {
    return 't';
  } else if (a.equals(ace_w)) {
    return 'w';
  } else if (a.equals(ace_x)) {
    return 'x';
  } else if (a.equals(ace_y)) {
    return 'y';
  } else if (a.equals(ace_z)) {
    return 'z';
  } else if (a.equals(ace_1)) {
    return '1';
  } else if (a.equals(ace_2)) {
    return '2';
  } else if (a.equals(ace_3)) {
    return '3';
  } else if (a.equals(ace_4)) {
    return '4';
  } else if (a.equals(ace_5)) {
    return '5';
  } else if (a.equals(ace_6)) {
    return '6';
  } else if (a.equals(ace_7)) {
    return '7';
  } else if (a.equals(ace_8)) {
    return '8';
  } else if (a.equals(ace_9)) {
    return '9';
  } else if (a.equals(ace_0)) {
    return '0';
  } else if (a.equals(ace_period)) {
    return '.';
  } else if (a.equals(ace_comma)) {
    return ',';
  } else if (a.equals(ace_apostrophe)) {
    return '\'';
  } else if (a.equals(ace_question)) {
    return '?';
  } else if (a.equals(ace_exclamation)) {
    return '!';
  } else if (a.equals(ace_plus)) {
    return '+';
  } else if (a.equals(ace_minus)) {
    return '-';
  } else if (a.equals(ace_multiply)) {
    return '*';
  } else if (a.equals(ace_divide)) {
    return '/';
  } else if (a.equals(ace_equals)) {
    return '=';
  } else if (a.equals(ace_space)) {
    return ' ';
  } else if (a.equals(ace_no_match)) { //not part of the specification, but useful for functionality
    return '~';
  }
}

void serialAvailable() { //to reduce the code size slightly, otherwise unnecessary function
  while (!Serial.available()) { //waiting for activity (i.e. user input) on the serial line
  }
}

void setup() { //only Serial.begin should be in setup, others should have their own functions
  Serial.begin(9600);
}

void loop() {
#ifdef DEBUG
  Serial.print("[DEBUG] INPUT>> ");
#endif
  serialAvailable();
  String userInput = Serial.readString();
#ifdef DEBUG
  Serial.println(userInput); //used as part of the debug output since V0.2
#endif
  aceDecode(userInput);
}

void aceDecode(String m) { //main decoding function
  String output = "";
  String aceOutputRaw = "";
  String aceOutput = "";
  char firstChar = m.charAt(0);
  if (firstChar == '!' || firstChar == '+' || firstChar == '&') //pattern matching
  {
    int aceLength = m.length();
    for (int t = 0; t <= aceLength; t++) {
      char aceStrChar = m.charAt(t);
      if (aceStrChar != '_') {
        aceOutputRaw += aceStrChar;
      } else if (aceStrChar == '_') {
        aceOutput += aceDecode2Char(aceOutputRaw);
      }
    }
    Serial.println(aceOutput);
  } else {
#ifdef DEBUG
    Serial.println("DEBUG>> ACE format match!");
#endif
    int strLen = m.length();
#ifdef DEBUG
    Serial.print("[DEBUG] STRING LENGTH>> ");
    Serial.println(strLen);
#endif
    for (int i = 0; i < strLen; i++) {
      char strChar = m.charAt(i);
#ifdef DEBUG
      Serial.print("[DEBUG] CURRENT CHARACTER>> ");
      Serial.println(strChar);
#endif
      output += char2Ace(strChar);
      if (i != strLen - 1 && strChar != ' ') {
        output += "_";
        int strLen2 = output.length();
        char strChar2 = m.charAt(i + 1);
        if (strChar2 == ' ') {
          output.remove(strLen2 - 1);
        }
      }
    }
  }
  Serial.println(output); //final output for aceDecode
}


Any ideas?

BulldogLowell

have you thought about using arrays?  It would reduce your code by about 2/3rds.

Something like:
Code: [Select]
struct ACE_Encode{
  const char* aceString;
  char character;
}


and then the array

Code: [Select]
ACE_Encode ace{
  {"+",      'a'},
  {"!!+",    'b'}.
  ... erc
};


then just iterate over the array...

Code: [Select]
String decodeAceString(const char* string, char delimiter)
{
  String result;
  while(string)
  {
   ... etc...

  

Go Up