Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!bcm!shell!nuchat!abbadon From: abbadon@nuchat.UUCP (David Neal) Newsgroups: comp.lang.c Subject: Re: Packed Decimal Conversion fns sought, TFM is silent Summary: A quick routine Keywords: PACKED DECIMAL IBM Message-ID: <11810@nuchat.UUCP> Date: 28 Jun 89 21:29:59 GMT References: <223@eds1.UUCP> Distribution: na Organization: South Coast Computing Services, Inc. - Houston, Tx Lines: 53 #include /* Routine to unpack BCD packed decimal into an integer. */ unpack(packed, length, unpacked) char packed[]; /* Array of chars containing packed number */ int length; /* Length of the array (not '\0' terminated) */ int *unpacked; /* Pointer to integer for return value */ { register int i, j; static char asciibuf[128]; /* Number cannot be > 63 digits in length */ j = 0; length--; /* Decrement length; we always use length - 1 because of the special case and because of 'C' array indices start at 0. */ /* Run to length-1 bytes; last byte is a special case */ for ( i = 0; i < length; i++ ) { asciibuf[j++] = ( (packed[i] & 0xf0) >> 4 ) + 0x30; asciibuf[j++] = (packed[i] & 0x0f) + 0x30; /* 0x30 is the delta between BCD nybble x and it's ascii counterpart. */ } asciibuf[j++] = ( (packed[length] & 0xf0) >> 4 ) + 0x30; asciibuf[j] = '\0'; fprintf(stderr, "unpack: %s \n", asciibuf); *unpacked = atoi(asciibuf); /* Special case: If the last nybble is 0x0d, the number is negative */ if ( (packed[length] & 0x0f) == 0x0d ) *unpacked *= -1; } Sorry about the wasted bandwith; this guys mail bounces and he's tough to get on the phone! Please no flamage about system dependant this or messy that; it's just a quick hack to help the guy. David Neal abbadon@nuchat.UUCP uunet!nuchat!abbadon killer!texbell!nuchat!abbadon