How to convert int array to string arduino

Hello , i have a question so here it goes
I have this code that is copied from this forum, it reads the interupts , i am trying to read wiegand protocol , and everything is working great , i have just one question , when i print the received bits as ints i print them with this code
` for (int i = 0; i < bits; i++)
{
Serial.print((int)DataBits[i]);

}

`

as you can see , they are printed as int , so how can i take all these bits and send them to a webserver thorugh get request, how can i convert them to string like this "00110011101010010" and send them

#define MAX_BITS 100                 // max number of bits 
#define WEIGAND_WAIT_TIME  500      // time (milliseconds) to wait for another Weigand pulse.  


uint8_t DataBits[MAX_BITS];    // stores all of the data bits
uint8_t BitCount;              // number of bits currently captured
boolean FlagDone = true;              // Set low when ready to capture data
volatile unsigned long WeigandWaitStartTime;  // countdown until we assume there are no more bits



// interrupt that happens when INTO goes low (0 bit)
void ISR_INT0()
{
  if (!FlagDone && BitCount < (MAX_BITS - 1))
  {
    
    DataBits[BitCount++] = 0;
    WeigandWaitStartTime = millis(); // Re-start the timer
  }
}


// interrupt that happens when INT1 goes low (1 bit)
void ISR_INT1()
{
  if (!FlagDone && BitCount < (MAX_BITS - 1))
  {
    DataBits[BitCount++] = 1;
    WeigandWaitStartTime = millis(); // Re-start the timer
  }
}


void setup()
{
  pinMode(13, OUTPUT);    // LED
  pinMode(2 , INPUT_PULLUP);      // DATA0 (INT0)
  pinMode(3, INPUT_PULLUP);      // DATA1 (INT1)

  pinMode(18, INPUT_PULLUP);      // DATA0 (INT0)
  pinMode(19, INPUT_PULLUP);   
  Serial.begin(9600);
  Serial.println("RFID Readers");

  // The two data lines idle HIGH
  if (digitalRead(2) == LOW) {
    Serial.println(F("WARNING: D0 line (on Pin 2) is not in a HIGH state.  Wiring error?"));
  }

  if (digitalRead(3) == LOW) {
    Serial.println(F("WARNING: D1 line (on Pin 3) is not in a HIGH state.  Wiring error?"));
  }


  // binds the ISR functions to the falling edge of INTO and INT1
  attachInterrupt(digitalPinToInterrupt(2), ISR_INT0, FALLING);
  attachInterrupt(digitalPinToInterrupt(3), ISR_INT1, FALLING);  // DATA1 (INT1)

    attachInterrupt(digitalPinToInterrupt(18), ISR_INT0, FALLING);
  attachInterrupt(digitalPinToInterrupt(19), ISR_INT1, FALLING);  // DATA1 (INT1)

  FlagDone = false;  // Allow the collection of data bits
}


void loop()
{
  unsigned long currentTime = millis();


  // Grab the
 noInterrupts();
  boolean done = FlagDone;
  uint8_t bits = BitCount;
  unsigned long timer = WeigandWaitStartTime;
  interrupts();


  //  If we have started receiving bits, wait until we have not had a new one for a while
  if (bits > 0)
  {
    digitalWrite(13, HIGH);
    if (currentTime - timer > WEIGAND_WAIT_TIME)
    {
      noInterrupts();
      FlagDone = true;  // Stop accepting data
      done = true;
      interrupts();
      
      digitalWrite(13, LOW);
    }
  }


  // if the timer completed, process the data
  if (done)
  {
    Serial.print("Read ");
    Serial.print(bits);
    Serial.println(" bits. ");


    


      // we will decode the bits differently depending on how many bits we have
      // see www.pagemac.com/azure/data_formats.php for mor info
      unsigned long facilityCode = 0;
      unsigned long cardCode = 0;


      if (BitCount == 35)
      {
      // 35 bit HID Corporate 1000 format
      // facility code = bits 2 to 14
      for (int i = 2; i < 14; i++)
      {
        facilityCode <<= 1;
        facilityCode |= DataBits[i];
      }


      // card code = bits 15 to 34
      for (int i = 14; i < 34; i++)
      {
        cardCode <<= 1;
        cardCode |= DataBits[i];
      }



      printBits(facilityCode,cardCode);
      }
      else if (BitCount == 26)
      {
      // standard 26 bit format
      // facility code = bits 2 to 9
      for (int i = 1; i < 9; i++)
      {
        facilityCode <<= 1;
        facilityCode |= DataBits[i];
      }


      // card code = bits 10 to 23
      for (int i = 9; i < 25; i++)
      {
        cardCode <<= 1;
        cardCode |= DataBits[i];
      }


      printBits(facilityCode, cardCode);
      }
      else
      {
      // you can add other formats if you want!
      Serial.println("Unable to decode.");
      }
    


    // Display the received bits
    for (int i = 0; i < bits; i++)
    {
      Serial.print((int)DataBits[i]);
      
          
    }

 
    // cleanup and get ready for the next card
    noInterrupts();
    BitCount = 0;
    FlagDone = false;  // Stop accepting data
    interrupts();
  }
}


void printBits(unsigned long facilityCode, unsigned long cardCode)
{
  // I really hope you can figure out what this function does
  Serial.print("FC = ");
  Serial.print(facilityCode);
  Serial.print(", CC = ");
  Serial.println(cardCode);
}

Not sure you really need the build up a string for that. Just do it the same way, print them but instead of sending to Serial, send them to the server.

To answer the question though, you can just build a cString (a null terminated char array)

byte bits[] = {1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1};
const byte bitsCnt = sizeof bits;
char bitString[bitsCnt + 1]; // +1 for trailing null

void setup() {
  Serial.begin(115200); Serial.println();
  for (byte i = 0; i < bitsCnt; i++) bitString[i] = (bits[i] == 0) ? '0' : '1'; // fill the cSring
  bits[bitsCnt] = '\0'; // terminate the cString
  Serial.print(F("bit string is: \"")); Serial.print(bitString); Serial.println(F("\"."));// print it
}

void loop() {}

if all goes well Serial Monitor (@ 115200 bauds) will show

bit string is: "1100011101011100101001".

just make sure you allocate enough space for the string in the buffer and add 1 char for the trailing null which denotes the end of the cString.

Thank you , that is exactly what i was looking for , Have a great day

have fun (don't create buffers if they are not needed, your arduino does not have so much SRAM)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.