Binary translator

I've been trying to create a binary to ASCII text converter using the arduino progamming (I know it's not the best for this, but it's the only one I know)

PS: I couldn't find any function that did that directely, so my plan was to compare the binary with a lot of switch...case's.

here is the code so far :

char dataReceived[450];
int numData = 450;
char tempData[450];
char ASCII[50];
bool newData = false;
int ts;
int index = 0;
void setup() {
  Serial.begin(9600);
}

void loop() {
  recvWithEndMarker();
  if (newData == true) {
    strcpy(tempData, dataReceived);
    parseData();
    index = 0;
    Serial.print(ASCII[index]); //Print some of the string to see if it's empty or not
    Serial.print(ASCII[7]);
    Serial.println(" ");
    newData = false;
  }
 }
void recvWithEndMarker() {
  //Made by Robin2
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      dataReceived[ndx] = rc;
      ndx++;
      if (ndx >= numData) {
        ndx = numData - 1;
      }
    }
    else {
      dataReceived[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void parseData() {
  char * strtokIndex;
  strtokIndex = strtok(tempData, " ");
  Serial.println(strtokIndex);
  ASCII[index] = atoi(strtokIndex);
  convertData();
  strtokIndex = strtok(NULL, " ");
  Serial.println(strtokIndex);
  while (strtokIndex != '\n') {
    ASCII[index] = atoi(strtokIndex);
    convertData();
    strtokIndex = strtok(NULL, " ");
  }
}

void convertData() {
  int tb = ASCII[index];
  if (tb = "01100001") {
    tb = 'a';
  }
  ASCII[index] = tb;
  index++;
}

As you can see, I've only added one case, just so it's easier to test
And it actually worked well with one binary. The case is : how to do it with MORE than one?

this part here :

void parseData() {
  char * strtokIndex;
  strtokIndex = strtok(tempData, " ");
  Serial.println(strtokIndex);
  ASCII[index] = atoi(strtokIndex);
  convertData();
  strtokIndex = strtok(NULL, " ");
  Serial.println(strtokIndex);
  while (strtokIndex != '\n') {
    ASCII[index] = atoi(strtokIndex);
    convertData();
    strtokIndex = strtok(NULL, " ");
  }
}

is meant to verify if there is any more binary to be converted

It can convert well up to the first call of the function convertData(), but I don't know how to make the arduino realise "oh, there is still more to convert" or "the array has ended, we can now show the results"

As of now, it transforms all of the elements of the string ASCII to 'a'
I've tried using sizeof, but it would give me back the total length of tempData (450)

example:

int strSize = sizeof(tempData);
if (strSize > 0) {
}

Serial.available() > 0 wouldn't work either, since the data has already stoped "flowing" at this point.

What can I do to solve this? Is there a workaround for this or is the arduino language simply not able to handle this kinda stuff?

tb is an integer, you can't compare that to a string. What you need is a cast.

void setup() {
  Serial.begin(57600);


  int tb = 0x61;
  Serial.println(tb, BIN);

  char tbAscii = (char)tb;
  Serial.println(tbAscii);
}

a binary to ASCII text converter

Do you mean a binary number?

If so, Serial.print(), sprintf() and itoa() et al. all work pretty well.

This goes well past my knowledge for arduino coding, so I think i’m just going to back out of this project until I learn some more

Thanks for the replies though, at least now I have some info on the issue

It may sound trite, but it's important to keep in mind that, underneith, its all binary. What I mean is, in the end, you are trying to convert one kind of binary representation to another. So if at any point it seems complex, step back and remember that, it can't really be that difficult because it's all ones and zeroes.

"Ones and zeroes? You don't know you were born. In my day we only had "I"s and "O"s and we didn't always have the "O"s..."

Can't remember who said that. May have been Whalley from the Dilbert cartoons.

Thank you for the reply

I get what you are saying, but the thing is, I’m pretty much a novice in terms of arduino programming, especially when it comes to data manipulation. So I will try to do what you’re saying, but after I become more experienced with this kind of stuff

As I understand it, you’re receiving your string as ‘0’ and ‘1’ characters over Serial.

Easiest is to convert that to an actual number. Iterate over the string, do some bit manipulation, and you have a number, which in turn easily turns into a character again.

Something like:

char s[8] = "01100001";

byte b = 0;

for (byte i=0; i<8; i++) {
  if (s[i] == '1') b |= 1 << (8-i);
}

Serial.print((char) b);

This code assumes the length of s is always 8 characters.

One major issue with your code: if you don’t get your “end marker” you’re going to overrun your string buffer. That’s bad. So make sure you don’t try to load more characters in that buffer than it’s long.