I need to write a C program to convert packed decimal field in a buffer to in integer value. The precision of packed decimal is 9 and Scale is 0. What is the best way to convert this in a IBM mainframe C progrram? In Cobol the format for Packed Decimal used is Comp-3 Any help is aprreciated.
Asked
Active
Viewed 1,613 times
1
-
There are are many questions related to Comp-3. But in Comp-3 123 is represented '123c' and -123 as '123d'. There are 2 approaches - convert to string and then to a number (see http://stackoverflow.com/questions/4337912/how-to-convert-unpacked-decimal-back-to-comp-3) or access the nybles (see http://stackoverflow.com/questions/142972/c-convert-comp-3-packed-decimal-to-human-readable-value) – Bruce Martin Apr 10 '14 at 22:16
2 Answers
1
If you are running the program on a Zos mainframe then the C compiler supports packed decimal natively.
Google for "Zos C fixed point decimal type" should get you the right manual page its a simple as :
#include <decimal.h>
decimal(9,0) mynum;

James Anderson
- 27,109
- 7
- 50
- 78
-
1Thanks for your response. But I think it will not resolve the question.I have a buffer as char * that needs to be converted to decimal(9,0), And I am not sure if can not do: decimal(9,0) mynum = charBuffer; or decimal(9,0) mynum = (decimal(9,0))charBuffer; – Amit Sood Apr 11 '14 at 21:10
1
The one way I think it can be done, is
long long MyGetPackedDecimalValue(char* pdIn, int length)
{
// Convert packed decimal to long
const int PlusSign = 0x0C; // Plus sign
const int MinusSign = 0x0D; // Minus `enter code here`
const int NoSign = 0x0F; // Unsigned
const int DropHO = 0xFF; // AND mask to drop HO sign bits
const int GetLO = 0x0F; // Get only LO digit
long long val = 0; // Value to return
printf ("in side ****GetPDVal \n ");
for(int i=0; i < length; i++)
{
int aByte = pdIn[i] & DropHO; // Get next 2 digits & drop sign bits
if(i == length - 1)
{ // last digit?
int digit = aByte >> 4; // First get digit
val = val*10 + digit;
printf("digit= %d, val= %lld \n",
digit,
val);
int sign = aByte & GetLO; // now get sign
if (sign == MinusSign)
{
val = -val;
}
else
{
// Do we care if there is an invalid sign?
if(sign != PlusSign && sign != NoSign)
perror("SSN:Invalid Sign nibble in Packed Decimal\n");
}
}
else
{
int digit = aByte >> 4; // HO first
val = val*10 + digit;
printf("digit= %d, val= %lld \n",
digit,
val);
digit = aByte & GetLO; // now LO
val = val*10 + digit;
printf("digit= %d, val= %lld \n",
digit,
val);
}
}`enter code here`
printf ("**** coming out GetPDVal \n ");
return val;
}

Amit Sood
- 385
- 3
- 7