我已经发布了一些关于我的项目的问题,这一切都很有帮助,但我仍然不确定其中的一部分。
我正在创建一个将ASCII转换为ASH并将ASH转换为ASCII的转换器。我现在已成功将ASCII转换为ASH,但无法从ASH转换为ASCII。我创建了一个名为' ASCIIstring2ASHstring'的函数。哪个工作正常,但我现在必须创建另一个名为' ASHstring2ASCIIstring'的函数。但是,我已经被告知,对于这个功能,我需要将用户输入的每个字符保存到临时缓冲区中,直到' /'出现,此时我需要通过调用' ASHstring2ASCIIstring'来打印出临时缓冲区中的所有字符作为完整字符串。功能(我现在要求帮助创建)。但是,我不知道我是怎么做到这一点的,我希望这里有人可以帮我一把:)
谢谢!
更新:我现在已经在一些帮助下创建了该函数,但由于某种原因它没有按预期打印出ASH到ASCII的翻译,我已经在下面发布了更新的代码。
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 ASH_1[] = "@!!*";
//char ASH_2[] = "@!!@";
//char ASH_3[] = "@!*!";
//char ASH_4[] = "@!**";
//char ASH_5[] = "@!*@";
//char ASH_6[] = "@!@!";
//char ASH_7[] = "@!@*";
//char ASH_8[] = "@!@@";
//char ASH_9[] = "@*!!";
//char ASH_0[] = "@!!!";
//char ASH_.[] = "@!!";
//char ASH_,[] = "@!*";
//char ASH_?[] = "@**";
//char ASH_![] = "@*@";
//char ASH_+[] = "@@!";
//char ASH_-[] = "@@*";
//char ASH_*[] = "@@@";
//char ASH_/[] = "@!@";
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);
// Serial.println(ASH2ASCII("!**")); //These are the ASH characters I want to convert to ASCII using the ASH2ASCII function
//Serial.println(ASH2ASCII("!"));
//Serial.println(ASH2ASCII("!*@"));
//Serial.println(ASH2ASCII("!*@"));
//Serial.println(ASH2ASCII("@*"));
//ASCIIstring2ASHstring (t, ob, oblen);
//Serial.println(ob);
usinput(buffer1);
Serial.print(buffer1);
chardecide(buffer1);
}
void chardecide(char * buffer1) { //char decide which acts upon the result of isASH using the true and false returns
if (isASH (buffer1)) {
Serial.println(" has been recognized as ASH - ");
ASHstring2ASCIIstring(buffer1, ob); //passes function with buffer1 and ob included
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) //converts ash to ascii
{
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) { //converts ascii to ash
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() {
}
答案 0 :(得分:1)
在<string.h>
中,有一个函数strtok
可以在给定字符范围内拆分字符串。您可以像这样使用它:
int ASHstring2ASCIIstring(char *buffer, char *ob)
{
char *token;
int l = 0;
token = strtok(buffer, "/");
while (token) {
println(token);
ob[l++] = ASH2ASCII(token); // Note: No overflow check!
token = strtok(NULL, "/");
}
ob[l] = '\0';
return l;
}
在第一次调用时,您提供要拆分的字符串,在后续调用中传递NULL
以告诉strtok
继续处理相同的字符串。此代码会破坏进程中的原始字符串,因为它会将结束标记放在标记的末尾。
您也可以通过填充辅助缓冲区来自行编码。当你点击斜杠时,处理缓冲区并清空它:
int ASHstring2ASCIIstring(char *buffer, char *ob)
{
char str[10];
int j = 0;
int l = 0;
while (*buffer) {
if (*buffer == '/') {
str[j] = '\0';
ob[l++] = ASH2ASCII(str);
j = 0;
} else {
if (j + 1 < sizeof(str)) {
str[j++] = *buffer;
}
}
buffer++;
}
ob[l] = '\0';
return l;
}
此代码保持字符串不变。它还要求在最后一个令牌后面有一个斜杠。这可能不是你想要的,但它可能是一个很好的起点。