-1

I'm writing a sketch for the Arduino board and am using the following C++ code to do so. I'm trying to convert decimal numbers to binary by passing a decimal through the dec2bin function which is to return a character array that I will then print out. However, I'm getting the error:

"Incompatible types of assignment of 'char' to 'char [0]'"

at the function call to dec2bin and I'm receiving another error at the return inside of the dec2bin function that says:

"Invalid conversion from 'char*' to 'char' [-fpermissive]"

If anyone could please assist me with this it would be greatly appreciated. I need to use a character array here and not a string! Thank you!

void loop() {
// put your main code here, to run repeatedly:
 if (Serial.available() > 0){
    char BinaryNum [0];
    int Decimal = Serial.parseInt();
    BinaryNum = dec2bin(Decimal);
    Serial.println (BinaryNum);

 }

}

char dec2bin (int Decimal){

  int Remainder; // Remainder of Decimal%2
  char Binary [0]; // Character array returned by dec2bin
  int x = 0;

  while (Decimal != 0 ){
    Remainder = Decimal%2; 
    Decimal = Decimal/2;
    Binary[x] = Remainder; 
    Serial.println(Binary[x]);
    x+=1;
  }

  return Binary;
}
Heisenberg
  • 27
  • 1
  • 12
  • 2
    Do not return local variable, in this case `Binary` . Allocate and return. – phoxis Apr 11 '16 at 23:09
  • 4
    `char Binary [0]; ` is an array of length 0 (which in itself is weird enough). Never minding its length, you *are* attempting to return an array where you promised you'd return a single `char`. Fixing only that is not enough, there are other problems with your code. – Jongware Apr 11 '16 at 23:09
  • 1
    A simple way to convert to binary digits is to use a `std::bitset`. I'm aware that standard library support is a bit lacking by default on the Arduino, but I recall from earlier Arduiono questions that such support is available from third parties. Including some GitHub projects. – Cheers and hth. - Alf Apr 11 '16 at 23:23

2 Answers2

1

Danger Danger, you're returning a pointer to a local variable, Binary, that is on the stack, once the function returns it is out of scope and no longer valid. This will cause weirdness, it will work some times and then it will stop working, don't do it!

See Can a local variable's memory be accessed outside its scope?

You need to pass in the storage.

eg.

void loop() {
// put your main code here, to run repeatedly:
 if (Serial.available() > 0){
    char BinaryNum[33]; // allows for 32 bits plus null terminator
    int Decimal = Serial.parseInt();
    dec2bin(Decimal, BinaryNum);
    Serial.println (BinaryNum);

 }

}

void dec2bin (int Decimal, char* Binary){

  int Remainder; // Remainder of Decimal%2
  int x = 0;

  while (Decimal != 0 ){
    Remainder = Decimal%2; 
    Decimal = Decimal/2;
    Binary[x] = Remainder; 
    x+=1;
  }
  Binary[x] = '\0';
}

On a "real" computer with an operating system there are many more memory management options than on a little arduino. You could allocation from the heap and use something fancy like an auto_ptr to manage it (see What is a smart pointer and when should I use one?).

This just isn't possible on something as small as an arduino. You could however allocate it as a static. This isn't appropriate for a fancy computer because it is not re-entrant and thus not thread safe. Static allocation is done at program linking or startup and persistent across calls.

void loop() {
// put your main code here, to run repeatedly:
 if (Serial.available() > 0){
    int Decimal = Serial.parseInt();
    char* BinaryNum  = dec2bin(Decimal);
    Serial.println (BinaryNum);

 }

}

void dec2bin (int Decimal) {
  static Binary[33]; // allows for 32 bits plus null terminator

  int Remainder; // Remainder of Decimal%2
  int x = 0;

  while (Decimal != 0 ){
    Remainder = Decimal%2; 
    Decimal = Decimal/2;
    Binary[x] = Remainder; 
    x+=1;
  }
  Binary[x] = '\0';

  return Binary;
}
Community
  • 1
  • 1
TomKeddie
  • 185
  • 1
  • 10
  • I appreciate this answer and it does clarify what I was doing wrong quite a bit. However, is there anyway to have the function return the array instead of passing it through the parameters or is this kind of just what needs to be done? Once again, thanks! – Heisenberg Apr 12 '16 at 02:31
0

I'm going to assume Serial.println() accepts a nul-terminated char*:

The signature for dec2bin should be char* dec2bin(int decimal), where char* is a pointer to the beginning of your char array.

Then you need to declare and allocate char* binary = new char[arraySize];. Note that the array cannot be re-sized. (Don't forget to free this memory later with delete[]). You may also want to add '\0' to the end of your array.

See this post if you want to return the array itself instead of a pointer (not recommended).

Community
  • 1
  • 1
Dafang Cao
  • 830
  • 4
  • 14