0
    struct FILE_UPC_RECORD
    {
     char UPC[FILE_UPC_KEY_SIZE];// packed up to 16 digits right justified and zero filled
                               // possibilities are:
                               // 1. 12-digit UPC w/2 leading 0's
                               // 2. 13-digit EAN w/1 leading 0
                               // 3. 14-digit EAN
        char SKU[FILE_ITEM_KEY_SIZE];  // packed, 0000ssssssssssss
    };

Where FILE_UPC_KEY_SIZE & FILE_ITEM_KEY_SIZE = 8. Is packed value equivalent to Hex value? How do i store '0123456789012' equivaent decimal calue in the UPC & SKU array? Thanks for all your help.

Ravi
  • 163
  • 1
  • 2
  • 12
  • "digit" usually means base 10. "packed" does not imply any base. – stark Dec 02 '13 at 22:51
  • Since the arrays are 8 bytes and they store up to 16 digits, it appears that "packed" means BCD (binary coded decimal). Another packing method could be used, but BCD makes the most sense in this case. – BitBank Dec 02 '13 at 23:55
  • I think it's not BCD. If it is a BCD, array size should have been 32 not 8. – Ravi Dec 03 '13 at 01:18
  • 1
    @user3043678: **16** BCD digits requires **8** bytes of storage, since a BCD digit is 4 bits long (*packed BCD*). – Jigsore Dec 03 '13 at 01:32
  • How do i do the conversion? Can anyone help in converting? – Ravi Dec 03 '13 at 17:45

1 Answers1

1

You asked "How do i ...", here's some example code with comments

#include <stdio.h>
#include <stdint.h>

int main(int argc, const char * argv[])
{
    int     i, newSize;

    // Treat the result packed data as unsigned char (i.e., not a string so not
    // NULL terminated)
    uint8_t upc[8];
    uint8_t *destPtr;

    // Assume input is a char string (which will be NULL terminated)
    char    newUPC[] = "0123456789012";
    char    *srcPtr;

    // -1 to remove the string null terminator
    // /2 to pack 2 decimal values into each byte
    newSize = (sizeof(newUPC) - 1) / 2;

    // Work from the back to the front
    // -1 because we count from 0
    // -1 to skip the string null terminator from the input string
    destPtr = upc + (sizeof(upc) - 1);
    srcPtr  = newUPC + (sizeof(newUPC) - 1 - 1);

    // Now pack two decimal values into each byte.
    // Pointers are decremented on individual lines for clarity but could
    // be combined with the lines above.
    for (i = 0; i < newSize; i++)
    {
        *destPtr  = *srcPtr - '0';
        srcPtr--;
        *destPtr += (*srcPtr - '0') << 4;
        srcPtr--;
        destPtr--;
    }

    // If input was an odd lenght handle last value
    if ( (newSize * 2) < (sizeof(newUPC) - 1) )
    {
        *destPtr = *srcPtr - '0';
        destPtr--;
        i++;
    }

    // Fill in leading zeros
    while (i < sizeof(upc))
    {
        *destPtr = 0x00;
        destPtr--;
        i++;
    }

    // Print the hex output for validation.
    for (i = 0; i < sizeof(upc); i++)
    {
        printf("0x%02x ", upc[i]);
    }

    printf("\n");

    return 0;
}
brad b
  • 21
  • 5
  • I appreciate your quick response and i have a question. Is 'uint8_t upc[8]' equal to 'char UPCCH[8]'? Can i do assignment: UPCCH = UPC? – Ravi Dec 03 '13 at 18:36
  • You can see the definition of uint8_t in stdint.h, but it is an unsigned char. – brad b Dec 03 '13 at 18:51
  • You can change this code to use a char, but the printf will sign extend the output unless you first cast it to unsigned char. So you you change the uint8_t to char, change the printf to printf("0x%02x ", (unsigned char) upc[i]); – brad b Dec 03 '13 at 18:59
  • Can i use the same code to convert from packed BCD to Decimal? – Ravi Dec 03 '13 at 20:53
  • You would have to reverse the operations, and if you wanted an int instead of a string you would have to do something like... (can't get it to format correctly but I think you'll get he point). value = 0; destPtr = upc; for (i = 0; i < sizeof(upc); i++) { value *= 10; value += (*destPtr >> 4) & 0x0f; value *= 10; value += *destPtr & 0x0f; destPtr++; } – brad b Dec 03 '13 at 22:44
  • I need the the packed BCD value in the structure member 'char UPC[FILE_UPC_KEY_SIZE]'. How to move the data from 'unit8_t' to 'char'? – Ravi Dec 04 '13 at 18:55
  • ... on common computers with e. g. `memcpy()`. – Armali Nov 04 '14 at 12:59