title: How to transform Wiegand card ID into a Wiegand binary?
body: <p><a href="https://en.wikipedia.org/wiki/Wiegand_interface" rel="nofollow noreferrer"><strong>Wiegand Interface:</strong></a></p>
  
  <ul>
  <li>a card ID is made of a Facility Code and a Card Number;</li>
  <li>the Facility Code represents the first 3 digits: <code>000</code></li>
  <li>the Card Number represents the last 5 digits: <code>99999</code></li>
  <li>now, we should take these numbers and transform them into binary and <strong>here lies the problem</strong></li>
  <li>the Facility Code needs to be an 8 bit long binary</li>
  <li>the Card Number needs to be an 16 bit long binary</li>
  <li>99999 can't be represented on 16 bits.</li>
  <li>what am I supposed to do now ?</li>
  </ul>
  
  <p>I have made a class that takes any decimal and transforms it into a Card ID with its respective Facility Code and Card Number:</p>
  
  <pre><code>class Wiegand {
    private:
      std::vector&lt;bool&gt; wiegandArray = std::vector&lt;bool&gt;(26, false);
      uint8_t facilityCode;
      uint16_t cardNumber;
      void calculate(uint32_t dec) {
        // transform dec number into binary number using single bit shift operation
        // and store it in wiegandArray[]
        for (int i = 24; i &gt; 0; --i) {
          wiegandArray[i] = dec &amp; 1;
          dec &gt;&gt;= 1;
        }
        // check for parity of the first 12 bits
        bool even = 0;
        for(int i = 1; i &lt; 13; i++) {
          even ^= wiegandArray[i];
        }
        // add 0 or 1 as first bit (leading parity bit - even) based on the number of 'ones' in the first 12 bits
        wiegandArray[0] = even;
  
        // check for parity of the last 12 bits
        bool odd = 1;
        for(int i = 13; i &lt; 25; i++) {
          odd ^= wiegandArray[i];
        }
        // add 0 or 1 as last bit (trailing parity bit - odd) based on the number of 'ones' in the last 12 bits
        wiegandArray[25] = odd;
      }
    public:
      Wiegand(uint32_t id) {
        calculate(id);
        facilityCode = id &gt;&gt; 16;
        cardNumber = id &amp; 0xffff;
      }
      //returns a 26 length std::vector&lt;bool&gt;
      std::vector&lt;bool&gt; getWiegandBinary() {
        return wiegandArray;
      }
      //returns uint8_t facilityCode
      uint8_t getFacilityCode() {
        return facilityCode;
      }
      //returns uint16_t cardNumber
      uint16_t getCardNumber() {
        return cardNumber;
      }
      //returns an 8 characters long String
      String getCardID() {
        String s1 = String(facilityCode);
        String s2 = String(cardNumber);
        String s3 = String(facilityCode);
        while(s1.length() &lt; 3) {
          s1 = String(0) + s1;
        }
        while(s2.length() &lt; 5) {
          s2 = String(0) + s2;
        }
        String s3 = s1 + s2;
        return s3;
      }
  };
  </code></pre>
  
  <p>The problem is that a Wiegand Central will return the Card ID when you scan a card, not the Wiegand representation in Decimal. So I have to preserve that format. I need some hocus-pocus that takes <code>00099999</code> as a Card ID and not as a decimal representation as my <code>Wiegand class</code>.</p>
  
  <p>Decimal <code>99999</code> is <code>000000011000011010011111</code> in 24 bit representation. This translates into:</p>
  
  <ul>
  <li>Facility Code: <code>00000001</code> or <code>1</code>;</li>
  <li>Card Number: <code>1000011010011111</code> or <code>34463</code>;</li>
  <li>That means Card ID: <code>00134463</code>.</li>
  </ul>
  
  <p>I am trying like crazy to make an algorithm that takes the Card ID <code>00099999</code> and transform it so it can give me Facility Code: <code>000</code> and Card Number <code>99999</code> and then make this into a proper 26 bit Wiegand.</p>
  
  <p><strong>Right now I think its impossible to do this.</strong>
  What do you think ?</p>
  
  <p>EDIT:</p>
  
  <p>I think that card number <code>00099999</code> cannot even exist. I have not talked to any card vendor and I think that I should have because I am missing vital information regarding the format of a Card ID. I think that the Card ID can only be made as a concatenation of the <strong>Facility Code</strong> that ranges from 0 to 255 and <strong>Card Number</strong> that ranges from 0 to 65535. So the "biggest" Card ID that can exist is <code>25565535</code>.
  If this is true, the input should be comprised of the Facility Code (0-255) and Card Number (0 - 65535) that will then form the Card Number and which I can easily transform into a Wiegand 26 bit.</p>
  
  <p><a href="https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/" rel="nofollow noreferrer"><strong>String Class</strong></a></p>