BINARY OUTPUT FORMAT

Hello, sorry for my ignorance, but I have little time working with arduino.

/**

  • Read a card using a mfrc522 reader on your SPI interface
  • Pin layout should be as follows (on Arduino Uno):
  • MOSI: Pin 11 / ICSP-4
  • MISO: Pin 12 / ICSP-1
  • SCK: Pin 13 / ISCP-3
  • SS: Pin 10
  • RST: Pin 9
    */

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN, RST_PIN);

// Setup variables:
int serNum0;
int serNum1;
int serNum2;
int serNum3;
int serNum4;

void setup()
{
Serial.begin(9600);
delay(1000);
SPI.begin();
rfid.init();
}

void loop()
{

if (rfid.isCard()) {
if (rfid.readCardSerial()) {
if (rfid.serNum[0] != serNum0
&& rfid.serNum[1] != serNum1
&& rfid.serNum[2] != serNum2
&& rfid.serNum[3] != serNum3
) {
/* With a new cardnumber, show it. */
Serial.println(" ");
Serial.println("Card found");
serNum0 = rfid.serNum[0];
serNum1 = rfid.serNum[1];
serNum2 = rfid.serNum[2];
serNum3 = rfid.serNum[3];

//Serial.println(" ");
Serial.println("Cardnumber:");
Serial.print("HEX: ");
Serial.print(rfid.serNum[0],HEX);
Serial.print(", ");
Serial.print(rfid.serNum[1],HEX);
Serial.print(", ");
Serial.print(rfid.serNum[2],HEX);
Serial.print(", ");
Serial.print(rfid.serNum[3],HEX);
Serial.println(" ");

Serial.println("BIN: ");
Serial.println(rfid.serNum[0],BIN);
Serial.println(rfid.serNum[1],BIN);
Serial.println(rfid.serNum[2],BIN);
Serial.println(rfid.serNum[3],BIN);
Serial.println(" ");

}
else { }
}
}
// rfid.halt();
}

I have this code to read the serial number of an RFID card and I need to format the output in CODE BINARI four characters. That is, for the output:

Card found
cardNumber:
HEX: 1E, D, 82, FE
BIN:
11110
1101
10000010
11111110

I would need:

BIN:
00011110
00001101
10000010
11111110

Someone can help me ..

thanks

Hi.

http://forum.arduino.cc/index.php?topic=137395.0

http://forum.arduino.cc/index.php?topic=43791.0

Hope this helps.

Tom... :slight_smile:

Thanks Tom for your quick response, but can not find the solution in your post.
Once converted to binary I need four bits for each binary number (with leading zeros).

Here is a subroutine I use for displaying OOK data from RF remote controls. maybe you can adapt it to your needs.

void dec2binLong(unsigned long myNum, byte NumberOfBits) {
  if (NumberOfBits <= 32){
    myNum = myNum << (32 - NumberOfBits);
    for (int i=0; i<NumberOfBits; i++) {
      if (bitRead(myNum,31) == 1) 
      Serial.print("1");
      else 
      Serial.print("0");
      myNum = myNum << 1;
    }
  }
}

These functions are based on one of the examples in Tom's post.

void printBinaryNibble(byte value)
{
    value = value & 0b00001111;
    for (byte mask = 0x08; mask; mask >>= 1) 
    {
        Serial.print((mask & value) ? '1' : '0');
    }
}

void printBinaryByte(byte value)
{
    for (byte mask = 0x80; mask; mask >>= 1) 
    {
        Serial.print((mask & value) ? '1' : '0');
    }
}

void printBinaryUnsignedInt(unsigned int value)
{
    for (unsigned int mask = 0x8000; mask; mask >>= 1) 
    {
        Serial.print((mask & value) ? '1' : '0');
    }
}

void setup()
{
    
    Serial.begin(115200);
    printBinaryNibble(0x7);
    Serial.println();
    printBinaryByte(0x97);
    Serial.println();
    printBinaryUnsignedInt(0x1997);
    Serial.println();
}

void loop()
{
    
}

Thank you very much for your help. It's just what I needed ...

Short way to do it.

// Binary_Output

int value ;

void setup() {
  Serial.begin(9600);
  //   just cycle one time
  for (value = 0; value < 256; value++)
  {
    for (int j = 7; j >= 0; j--)
    {
      Serial.print(bitRead(value, j));
    }
    Serial.print(" ");
    Serial.println(value);
  }

}

void loop()
{
  value = 0;
}

Output:
00000001 1
00000010 2
00000011 3
00000100 4
00000101 5
00000110 6
00000111 7
00001000 8
00001001 9
00001010 10
00001011 11
00001100 12
00001101 13
00001110 14
00001111 15
00010000 16
00010001 17
00010010 18
00010011 19
00010100 20
00010101 21
00010110 22

Thank you steinie44