Wiegand Library - Extracting HEX to Variable


I am trying to make a simple access control system with a Weigand 26 reader and an arduino. I am currently using the test code to try and build this simple system as shown below:

#include <Wiegand.h>


char currentID[6] = {'0','0','0','0','0','0'};
char allowedID[6] = {'9','C','5','E','2','B'};
char unprocessed[] = "";
int i = 0;

void setup() {

void loop() {
		Serial.print("Current Tag HEX Code:");
    unprocessed = wg.getCode(), HEX;
    for(int i = 0; i <6; i++){
      if(currentID[i] == allowedID[i]){
      digitalWrite(13, HIGH);
      Serial.println("Access Granted");
      digitalWrite(13, LOW);
      Serial.println("Access Denied");

This code is very unfinished but mainly because I cannot figure out how to put the data from the wg.getCode(), HEX command into a string where I can then compare it against the allowedID array.

Any help would be really great!

I think it’s just a little confusion about arrays, long integer, hexadecimal, binary, etc… :slight_smile:

In your sketch, an int, a char or a long is just a number. It is stored in RAM as a set of 0’s or 1’s (binary), but you can just say

    i = 10;

This is stored as 0000000000001010 in 16 individual bits of RAM, but you don’t really see that binary form. You just wrote the decimal (base 10 form) of “10”. You could have written it as

    i = 0x0A;

This is the hexadecimal (base 16 form) of 1010 (base 10). And since int is a 16-bit integer on (most) Arduinos, you could have written this, too:

    i = 0x000A;

With 16 bits, you can represent signed numbers from -32768 to +32767, or unsigned numbers from 0 to 65535. For example, 102310 is 0x03FF (base 16). The “0x” prefix tells the compiler to interpret it as a hexadecimal number. The hexadecimal range is 0x0000 to 0xFFFF.

With 32 bits, you can represent 0x00000000 to 0xFFFFFFFF.

Normally, the “base” of the number doesn’t matter, unless you’re reading it from the Serial port, or you’re writing it to the Serial port. Base 10 is the default, so printing 10 appears as “10” in the Serial Monitor window.

The Serial Monitor window is interpreting bytes as ASCII characters, not a numeric value. In order to display a “10” in the window, the Serial.print function has to send a 49 byte and a 48 byte. It does that numeric-to-ASCII conversion for you.

The Weigand library returns the 32-bit numeric code from the reader. That will fit in a unsigned long integer, which is the return type for the getCode function. You can compare that with a 32-bit code like 0x009c5e2b, like this:

    if (wg.getCode() == 0x009c5e2b)
        Serial.println( "Welcome!" );

You can print the code in hex to the Serial Monitor window like this:

    unsigned long code = wg.getCode();
    Serial.print( "Code is 0x" );
    Serial.println( code, HEX );

In order to display the hex code, the Serial.print function converts the hexadecimal 32-bit value 0x009C5E2B (or 1024772310) to a 57 byte, a 67 byte, a 53 byte, a 69 byte, a 50 byte and a 66 byte. If you wanted to use these character literals in your sketch (you don’t), that would have been a ‘9’ (not 9), a ‘C’ (67, not 0x0C nor 12), etc. In a double-quoted string, that would be “9C5E2B”.

Those are all different “presentations” of the internal, binary values that are stored in a variable. Because the Serial Monitor is really interpreting bytes as an ASCII code, the Serial.print method converts values to a series of ASCII bytes.

Your program logic almost always uses values, so just compare them, 32 bits all at once:

unsigned long allowedID = 0x009c5e2b;

void loop()
    if (wg.available()) {
        unsigned long code = wg.getCode();
        if (code == allowedID) {
            Serial.println( "Welcome!" );
        } else {
            Serial.print( "Invalid code is 0x" );
            Serial.println( code, HEX );
    unprocessed = wg.getCode(), HEX;

What is the . HEX crap there for?

That's great makes sense now! Thanks!