Encoding a date into an ASCII barcode

Asked
Viewd2003

0

I am investigating encoding of date information into a linear barcode that must be as short as possible. One idea we have had is encoding date information rather than using the ASCII characters for the date numerals.

  1. The earliest date to be encoded is 01-JAN-2009
  2. Date must be encoded using printable ASCII characters

2 个答案

3

如果只考虑日期(不考虑时间):

  Take the date to be encoded.
 Subtract 1-1-2009 from it
 Get the amount of elapsed days.
 

要将这个数字编码为可打印的ascii字符(ascii 48-127)序列,请执行以下操作

伪代码:

  const int ASCIILOWRANGE = 48
 const int ASCIIHIGHRANGE = 126
 const char ASCIIENDMARKER = 127;


 String convertToAscii(int numberOfDays)
 {
       String output = "";

       while(numberOfDays>0)
       {
           output += (char) ASCIILOWRANGE + numberOfDays % (ASCIIHIGHRANGE-ASCIILOWRANGE);
           numberOfDays /= (ASCIIHIGHRANGE-ASCIILOWRANGE);
       }
       output += ASCIIENDMARKER ;
       return output;
 }

 //decoder
 int convertToDays(String ascii)
 {
      char rightmost;
      int numberOfDays = 0;
      while(ascii.length>0)
      {
       rightmost = ascii[0];
           if(rightmost == ASCIIENDMARKER ) break; //ready

           numberOfDays *= (rightmost - ASCIILOWRANGE) * (ASCIIHIGHRANGE-ASCIILOWRANGE);
           ascii = ascii.substr(1); //remove rightmost char from string
      }
      return numberOfDays ;
 }
 

这种编码方式是最密集的。由于它以2个字符的形式编码了长达96天的未来。 9216天,以3个字符为单位,884736天以4个字符。

  • why a leap year issue? can you elaborate? If the base date (1-1-2009) doesn’t change. Then X days after this date will always be the same date. Or am I missing something?

    ToadAugust 07, 2009 15:11
  • My point was that it seemed to fit a shorter range into the same number of characters. I see now that the terminator character is what makes the difference. Assuming the terminator is not required (date is always at the end of the barcode,) this algorithm does seem to be the tightest, reaching 884,767 (about 2,400 years) in three characters.

    Aidan RyanAugust 07, 2009 15:05
  • Caveat for future visitors of this answer: “days since day X” encodings are susceptible to leap year issues!

    Aidan RyanAugust 07, 2009 15:06
  • to visualise it better… it is like decimal uses base 10. Hex uses base 16. This method uses base 96 (96 printable ascii chars). so counting goes like this a,b,c,….. aa,ab,ac,ad,ae……..ba,bb,bc,bd,…. …… aaa,aab,aac,aad etc

    ToadAugust 07, 2009 15:04
  • yes… with every character the time goes up a factor 96. So for instance 8 characters would give us: 96^7 days = 205876021399 years. So it think it is future proof ;^)

    ToadAugust 07, 2009 15:02
  • This seems to be tighter than my idea above, but only seems to reach 24 years into the future with 3 characters. Am I figuring something wrong?

    Aidan RyanAugust 07, 2009 15:01
3

使用日期本身:在Code39样式条形码下,01/01/2009是完全可以接受的条形码。

如果您在读取过程中不介意处理逻辑,则可以执行一些操作,例如删除“ /”或使用的任何分隔符。

基本上,您可以将类似“ 01012009”的代码编码到条形码中,然后使解码过程只需读取前两个数字作为月份,然后读取两个数字作为日期,然后读取最后四个数字作为年份。由于它们是直接的ascii,因此无需任何真正困难的处理即可工作。

第二种选择是将2009年1月1日作为您的“ 0”日期,然后对之后的天数进行编码。解码过程将在该过程中读取数字,然后将该天增加到1/1/09。可以这样进行,即2009年5月5日将被编码为“ 4”,那么当您想读回日期时,您可以在09年1月1日添加4来获得1月5日。

  • if utilizing 8-digit date storage it makes optimum sense to store it as yyyymmdd soas to make it readable, sortable, and errorproof through the year 9999

    HardryvAugust 07, 2009 15:14