Search code examples
stringarduinolong-integerarduino-c++

String with binary to long and reverse. Unexpected results. Arduino


volatile long pakiet = 0;
String longToBinaryString(long num)
{
  String result = "";
  for (int i = 31; i >= 0; i--)
  {
    if ((num >> i) & 1)
    {
      result += "1";
    }
    else
    {
      result += "0";
    }
  }
  return result;
}


bool isNumber(String str)
{
  for (int i = 0; i < str.length(); i++)
  {
    if (!isdigit(str.charAt(i)))
    {
      return false;
    }
  }
  return true;
}

long readBinaryString(char *s) {
  long result = 0;
  while (*s) {
    result <<= 1;
    if (*s++ == '1') result |= 1;
  }
  return result;
}

long binaryToLong(String binaryString) {
  long result = 0;
  for (int i = 0; i < binaryString.length(); i++) {
    if (binaryString.charAt(i) == '1') {
      result += pow(2, binaryString.length() - i - 1);
    }
  }
  return result;
}

void setup() {
  Serial.begin(115200);
  Serial.println("yo");
}

void loop() {


  if (Serial.available() > 0)
  {
    String message = Serial.readStringUntil('\n');
    message.trim();
    if (isNumber(message) and message.length() == 32)
    {
      char charArray[33];
      message.toCharArray(charArray, 33);
      Serial.print("What enter the function: ");
      Serial.println(charArray);
      pakiet = readBinaryString(charArray);
      Serial.print("Thats exit the function: ");
      Serial.println(pakiet);
      Serial.print("Thats the reverse of the function: ");
      Serial.println(longToBinaryString(pakiet));
    }

    if (isNumber(message) and message.length() == 32)
    {
      Serial.print("What enter the function: ");
      Serial.println(message);
      pakiet = binaryToLong(message);
      Serial.print("Thats exit the function: ");
      Serial.println(pakiet);
      Serial.print("Thats the reverse of the function: ");
      Serial.println(longToBinaryString(pakiet));
    }
  }
}

The problem is the result.

I want to get same result for example data: '11111111000011111111111111000011'

Results are

  • 14:56:45.162 -> What enter the function: 11111111000011111111111111000011
  • 14:56:45.162 -> Thats exit the function: -15728701
  • 14:56:45.162 -> Thats the reverse of the
    function: 11111111000011111111111111000011
  • 14:56:45.162 -> What enter the function: 11111111000011111111111111000011
  • 14:56:45.162 -> Thats exit the function: -1089471104
  • 14:56:45.162 -> Thats the reverse of the function: 10111111000011111111110110000000

I want to make function that uses strings, to work as expected and be reversable.

Why function that using String is working differentyl than with *char aray?


Solution

  • long readBinaryString(char *s) {
      long result = 0;
      while (*s) {
        result <<= 1;
        if (*s++ == '1') result |= 1;
      }
      return result;
    }
    
    long binaryToLong(String binaryString) {//Just cast it to char array.
      char charArray[binaryString.length()+1];
      binaryString.toCharArray(charArray, binaryString.length()+1);
      long result = readBinaryString(charArray);
      return result;
    }
    

    As Robin Hellmers posted Yes. That solves the problem.