Can't find out what's wrong with my ASH to ASCII translator?

Hey guys, just changed the title from “How to store a buffer of single characters and print them out as a string” because I have created my new function ‘ASHstring2ASCIIstring’. However, the ASH to ASCII part of the translation doesn’t seem to be working, but I’m sure I’ve passed the function correctly. I have passed my ‘ASCIIstring2ASHstring’ in the exact same way and it works fine, so not sure why ash isn’t translated to ascii when the user enters it into the serial port but ascii is translated to ash fine.

Maybe one of you guys can see what I’ve done wrong? :slight_smile:

Here’s the code I have so far:

 char ASH_a[] = "*";
char ASH_b[] = "!!*";
char ASH_c[] = "!!@";
char ASH_d[] = "*!";
char ASH_e[] = "!";
char ASH_f[] = "!*!";
char ASH_g[] = "**";
char ASH_h[] = "!**";
char ASH_i[] = "!!";
char ASH_j[] = "*!!";
char ASH_k[] = "*@";
char ASH_l[] = "!*@";
char ASH_m[] = "!*";
char ASH_n[] = "!@";
char ASH_o[] = "@*";
char ASH_p[] = "!@!";
char ASH_q[] = "*!@";
char ASH_r[] = "!@*";
char ASH_s[] = "@!";
char ASH_t[] = "@";
char ASH_u[] = "@@";
char ASH_v[] = "**!";
char ASH_w[] = "***";
char ASH_x[] = "*@!";
char ASH_y[] = "!@@";
char ASH_z[] = "**@";
 
char t[] = "ash code";
char buffer1[100];
const int oblen = 100;
char ob [oblen];



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

   
  usinput(buffer1);
  Serial.print(buffer1);
  chardecide(buffer1);

}

void chardecide(char * buffer1) {
  if (isASH (buffer1)) {
    Serial.println(" has been recognized as ASH - ");
    ASHstring2ASCIIstring(buffer1, ob);
    Serial.println(ob);
} else {
    Serial.println(" has been recognized as ASCII - ");
    ASCIIstring2ASHstring (buffer1, ob, oblen);
    Serial.println(ob);
  }
}

void usinput(char * ib ) {
  char inChar;
  int i = 0;
  do {
    while (!Serial.available()) {};
    inChar = Serial.read();
    if (inChar == '\n') {
      break;
    } else {
      ib[i] = inChar;
      i++;
    }
    ib[i] = '\0';
  } while (true);
}


bool isASH(char * buffer1)
{

  if (buffer1[0] != '*' && buffer1[0] != '!' && buffer1[0] != '@') return false;

  return true;

}

 int ASHstring2ASCIIstring(char *buffer, char *ob)
{
    char str[10];
    int j = 0;
    int l = 0;

    while (*buffer) {
        if (*buffer == '/') { //hit slash
            str[j] = '\0'; //empty buffer
            ob[l++] = ASH2ASCII(str);
            j = 0;
        } else {
            if (j + 1 < sizeof(str)) {
                str[j++] = *buffer;
            }
        }

        buffer++;
    }

    ob[l] = '\0';
    return l;
}

void ASCIIstring2ASHstring (char * ip, char * op, int oplen) {

  op[0] = '\0';
  int bp = 0;
  int n;
  char m[9];
  int l = strlen(ip);
  for (int i = 0; i < l; i++) {
    m[0] = '\0';
    strcpy(m, ASCII2ASH(ip[i]));
    n = strlen(m);
    if ((bp + n + l) < oplen) {
      strcat(op , m);
      bp = bp + n;
    }
    if (ip[i] != ' ' && ip[i + l] != ' ') {
      op[bp] = '/';
      bp++;
    }
    op[bp] = '\0';
  }
}


char ASH2ASCII(char * m) { //Using the char variables impmented globally, ASH2ASCII searches through specific variables until it finds a match for the conversion, at which point it will capture and reuturn the ASCII string

  if (strcmp(ASH_a, m) == 0) { //if string captured return a
    return 'a';
  }
  else if (strcmp(ASH_b, m) == 0) { //else if b string is captured return
    return 'b';
  }
  else if (strcmp(ASH_c, m) == 0) {
    return 'c';
  }
  else if (strcmp(ASH_d, m) == 0) {
    return 'd';
  }
  else if (strcmp(ASH_e, m) == 0) {
    return 'e';
  }
  else if (strcmp(ASH_f, m) == 0) {
    return 'f';
  }
  else if (strcmp(ASH_g, m) == 0) {
    return 'g';

  }
  else if (strcmp(ASH_h, m) == 0) {
    return 'h';

  }
  else if (strcmp(ASH_i, m) == 0) {
    return 'i';

  }
  else if (strcmp(ASH_j, m) == 0) {
    return 'j';

  }
  else if (strcmp(ASH_k, m) == 0) {
    return 'k';

  }
  else if (strcmp(ASH_l, m) == 0) {
    return 'l';

  }
  else if (strcmp(ASH_m, m) == 0) {
    return 'm';

  }
  else if (strcmp(ASH_n, m) == 0) {
    return 'n';

  }
  else if (strcmp(ASH_o, m) == 0) {
    return 'o';

  }
  else if (strcmp(ASH_p, m) == 0) {
    return 'p';

  }
  else if (strcmp(ASH_q, m) == 0) {
    return 'q';

  }
  else if (strcmp(ASH_r, m) == 0) {
    return 'r';

  }
  else if (strcmp(ASH_s, m) == 0) {
    return 's';

  }
  else if (strcmp(ASH_t, m) == 0) {
    return 't';

  }
  else if (strcmp(ASH_u, m) == 0) {
    return 'u';

  }
  else if (strcmp(ASH_v, m) == 0) {
    return 'v';

  }
  else if (strcmp(ASH_w, m) == 0) {
    return 'w';

  }
  else if (strcmp(ASH_x, m) == 0) {
    return 'x';

  }
  else if (strcmp(ASH_y, m) == 0) {
    return 'y';

  }
  else if (strcmp(ASH_z, m) == 0) {
    return 'z';

  }

}

void ASCIIstring2ASH (char * buf) {

  Serial.println(ASCII2ASH(*t));

}


char * ASCII2ASH (char c) { //This is the opposire of ASH2ASCII, it uses the globally defined variables to search through ASCII characters, and returns the ASH version of that character

  switch (c) {

    case 'a':
      return ASH_a;//returns ASH version of a if matched

    case 'b':
      return ASH_b;

    case 'c':
      return ASH_c;

    case 'd':
      return ASH_d;

    case 'e':
      return ASH_e;

    case 'f':
      return ASH_f;

    case 'g':
      return ASH_g;

    case 'h':
      return ASH_h;

    case 'i':
      return ASH_i;

    case 'j':
      return ASH_j;

    case 'k':
      return ASH_k;

    case 'l':
      return ASH_l;

    case 'm':
      return ASH_m;

    case 'n':
      return ASH_n;

    case 'o':
      return ASH_o;

    case 'p':
      return ASH_p;

    case 'q':
      return ASH_q;

    case 'r':
      return ASH_r;

    case 's':
      return ASH_s;

    case 't':
      return ASH_t;

    case 'u':
      return ASH_u;

    case 'v':
      return ASH_v;

    case 'w':
      return ASH_w;

    case 'x':
      return ASH_x;

    case 'y':
      return ASH_y;

    case 'z':
      return ASH_z;

    case ' ':
      return " ";

    default:
      Serial.println("switching went wrong!");
      break;

  }
}



void loop() {


}

If you kept all your questions about this project in a single Thread the people who helped you already would know what you are about and would have all the earlier info at their fingertips.

...R

Robin2: If you kept all your questions about this project in a single Thread the people who helped you already would know what you are about and would have all the earlier info at their fingertips.

...R

Sorry, I'm not sure how to do that :(

char ashTable[26][4] = {"*", "!!*", "!!@", "*!", "!", "!*!", "**", "!**", "!!", "*!!", "*@", "!*@", "!*", "!@", "@*", "!@!", "*!@", "!@*", "@!", "@", "@@", "**!", "***", "*@!", "!@@", "**@"};
char* testAscii = "hello world";
char testAsh[50] = "";

void setup(){
  Serial.begin(115200);
  for(char* c =  testAscii; *c; c++){//convert ascii string to ash string
    strcat(testAsh, ascii2ash(*c));
    strcat(testAsh, " ");
  }//for(c)
  Serial.println(testAsh);
  testAscii = "";//reset string
  char *r = testAsh;
  char *s = testAscii;
  while(*r) *s++ = ash2ascii(strsep(&r, " "));//convert ash string to ascii string
  *s = 0;
  Serial.println(testAscii);
}//setup()

void loop(){}//loop()

char* ascii2ash(char c){
  if(c==' ') return " ";
  return ashTable[toupper(c)-'A'];
}//ascii2ash()

char ash2ascii(char *s){
  if(*s==0)return ' ';
  for(int i = 0; i<26; i++) if(!strcmp(s, ashTable[i])) return i+'a';
  return -1;
}//ash2ascii()

just a fyi, watch out for the !, three in a row and the mega 2560 freaks out.

jasit: just a fyi, watch out for the !, three in a row and the mega 2560 freaks out.

The program puts a / after every character so you can distinguish between each character in a word because ascii can be pretty confusing to read, and I'm using a standard arduino uno board.

I think I might need to use an array to store the characters now, and then print out a temporary buffer (where I'll store the full string of all the single characters built up) when I call the ASHstring2ASCIIstring. I'm just having trouble putting my thoughts into code :(

lewrossi: Sorry, I'm not sure how to do that :(

Very simple

Don't start new Topics

...R

You have been cheating on us, the marriage is off ;)

http://stackoverflow.com/questions/27257423/how-to-store-a-buffer-of-single-characters-and-print-them-out-as-a-string

LarryD: You have been cheating on us, the marriage is off ;)

http://stackoverflow.com/questions/27257423/how-to-store-a-buffer-of-single-characters-and-print-them-out-as-a-string

haha ;) need all the help I can get!