Comparing ASCII characters from a Serial port with an integer from a RTC

Really not sure on the best way to do this.

I have a barcode reader attached to the UART I then read in the async data from that a bit like this

            while (!mySerial.available());                  // Halt until at least 1 byte is received
            scantype[inCount] =;            // Write byte into array
            if (scantype [inCount] == LINEBREAK) break;     // Break out of loop when byte received is a CR
          } while(true);
            (inCount < INLENGTH);                         // Or if Input overflows
            scantype[inCount] = 0;                          // null terminate the string

where scantype is char scantype[16]
Naturally the contents of scantype is the ASCII bytes before being encoded rather than the ASCII characters of the barcode I have read.

This array of characters contains a barcode such as 1032013123456 where the 032013 bit is a date code for expiry of a product I am scanning So in this case 03/2013 or March 2013

Then I am using RTClib to get the current date from a DS1307 clock.

This comes out as a 16bit Integer for the Year and an 8 bit integer for the Month

I know how to move my pointer through the array to get to the date part of the string. But then I am comparing the byte value of an ASCII encoded character with an Integer representing a year or a month…eek

Any ideas on how best to go about this…I want to determine whether the read date is older than the current month and year (i.e the product I am scanning has expired)

Here's one way to extract month and year from the barcode string:

int xtract(char *s,int n)
{   int v = 0;
    while (n--) v = 10 * v + *s++ - '0';
    return v;

char *barcode = "1032013123456";

int month = xtract(barcode+1,2);
int year  = xtract(barcode+3,4);

Edit: added semicolon

One way to do it, as long as you don’t need the barcode for anything else, is to put a null in the barcode after the year and use atoi to convert it. Then put a null just after the month and do atoi on that.
Something like this:

  scantype[7] = 0;
  year = atoi(&scantype[3]);
  scantype[3] = 0;
  month = atoi(&scantype[1]);

BTW. This bit of code doesn’t do what you say:

          } while(true);
            (inCount < INLENGTH);                         // Or if Input overflows

the second line doesn’t do anything. I think what you intended was this:

          } while(true && (inCount < INLENGTH));                         // Or if Input overflows


I think Morris's method is very clean and simple with no barcode changes.

Thanks guys,

That really helps and good spot on the other bits.

The only thing I do with the barcode after that is split it up to be printed as Expiry Month Year and Batch code on a receipt printer. So actually shoving in null characters during the validation will help rather than hinder me.

Thanks again for the quick responses...I have been scratching my head over this for a while and everything I came up with was about 4 times as complicated as your suggestions.