Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting packed decimal to number using C 1

Status
Not open for further replies.

rsteffler

Programmer
Aug 2, 2001
10
US
Does anyone have any C code that can be used to convert packed decimal to decimal formats? I also need to convert decimal formats to packed decimal.

Thanks,
Robert
 
Forgive my ignorance but can you clarify what a packed decimal means.

Thanx,
SwapSawe.
 
Sure. Packed decimal format is used in COBOL to store numbers in half the number of bytes. Don't feel bad, I just learned what packed decimal format was when I couldn't read the number information in a mainframe output file.

For example, if I have the number 12345, it would be stored as the following hex sequence: 12 34 5C. In the file, the hex values are shown as all the ASCII characters that you could ever ask for.

I hope this helps (and I hope that this is the proper understanding).


 
What does the C in 12 34 5C mean? I thought 12345 would be represented by hex 01 23 45. How would 123456, -12345 and -123456 be represented? Here's a function which may be helpful. You will have to modify it to handle the "C".

#include <stdio.h>

void frompd(pd,len,num)
char *pd;
int len, *num;
{
int i,j, n1;
j=1;
*num = 0;
for (i=len-1;i>=0;i--)
{
n1 = (i%2) ? pd[i/2] % 16 : pd[i/2] / 16;
*num += n1*j;
j *= 10;
}
}

main()
{
int num;
char packed[2];
packed[0] = 0x64;
packed[1] = 0x42;
frompd(packed, 4, &num);
printf(&quot;num= %d\n&quot;,num);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top