UPD Mail Archive: UPD> Unique ID: String->Integer

UPD> Unique ID: String->Integer

From: Jim Sommer (jsommer@bellatlantic.net)
Date: Thu Oct 04 2001 - 11:52:13 EDT

  • Next message: Jim Sommer: "UPD> item 10"

    I had been just assigning consecutive integer values to each unique ID.
    This worked fine but I ran into some situations (installing/uninstalling
    options) where the integer ID changed for a specific unique ID string. This
    meant that any integer ID that I had saved in the driver was no longer valid.

    I was looking for an algorithm to convert the unique ID strings into unique
    ID integers. I don't really need all ID integers to be unique, just all
    those in the same list (MediaSizeList, MediaTypeList, etc) need to be
    unique. I also need them to be consistent even if new ID's are added or
    some disappear.

    Norbert suggested the algorithm used to generate the LPTENUM checksum. I
    hunted down the algorithm and found that it generates unique ID integers
    for all the ID strings in the sample implementation. I would be a little
    happier if it generated 32-bit ID integers, but so far it works fine.

    Here's the code:

    WORD wCRC16a[16]={
         0000000, 0140301, 0140601, 0000500,
         0141401, 0001700, 0001200, 0141101,
         0143001, 0003300, 0003600, 0143501,
         0002400, 0142701, 0142201, 0002100,
    };
    WORD wCRC16b[16]={
         0000000, 0146001, 0154001, 0012000,
         0170001, 0036000, 0024000, 0162001,
         0120001, 0066000, 0074000, 0132001,
         0050000, 0116001, 0104001, 0043000,
    };

    short Get_CRC_CheckSum(void *pBuffer, ULONG ulSize)
    {
       BYTE *pb;
       BYTE bTmp;
       ULONG ulSeed=0;

       for (pb=(BYTE *)pBuffer; ulSize; ulSize--, pb++)
       {
         bTmp=(BYTE)(((WORD)*pb)^((WORD)ulSeed)); // Xor CRC with new char
         ulSeed=((ulSeed)>>8) ^ wCRC16a[bTmp&0x0F] ^ wCRC16b[bTmp>>4];
       }
       return (short)ulSeed;
    }

    The seed is set to 0 each time so that the ID will always be the same for a
    given string.

    If anyone has any comments or suggestions for a 32-bit CRC, I'd be happy hear.

    Jim

    mailto:sommer@granitesystems.com
    978-486-3068



    This archive was generated by hypermail 2b29 : Thu Oct 04 2001 - 11:54:06 EDT