You are here:

C/convert decimal to computational field of cobol using c

Advertisement


Question
hi,
the computational field in cobol saves a number in binary but in compressed form i.e. +999 or -999 is stored in 2 bytes instead of 3 bytes.
a detailed explanation is given on this link

http://www.simotime.com/databn01.htm
i need some help in understanding the concept explained and if some algorithm is available for the same.
i want to achieve the same with c code.

Answer
Hello muffaddal

In C and C++ integers are stored as binary data. It is different from cobol where numbers are stored as ascii or ebcdic character strings. As soon as you do a scanf into an integer, the value you input is in binary form, and is what cobol would call a computational field.

Now if you want to write a function to emulate atoi, or atol or scanf, and convert a text representation to a binary representation, it is not difficult to do. Here is some sample code. I'm sure you can follow it.

int main()
{
   char* text = "1234";
   int integer = 0;
   int mult = 1;
   int ix;
   for(ix = strlen(text)-1; ix >= 0; --ix)
   {
       char digit = text[ix] - '0';
       integer = integer + digit * mult;
       mult = mult * 10;
   }
   printf("text '%s' as integer is %d\n", text, integer);
}

If you want to prove to yourself that an integer is stored in binary format, you can do so by accessing each bit or byte and printing it out. For example
81985529216486895 (17 characters) is stored in 8 bytes.
Here are the bytes
0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF

You can write a program to access and print the individual bytes. It is a little tricky, because it involves bit shifting which is unusual for many people, but I've written a program for you below. Feel free to ask me a follow up question if there is some part you don't understand. I've used your idea of long long, but I've hidden it in a typedef to make it more convenient to type and change.


/* you can replace long long here with short or int if you like */
typedef long long dec_t;
typedef unsigned long long udec_t;


void printAsHex(dec_t decimal)
{
   printf("%lld as hex: ", (long long)decimal);
   /* mask starts out as 0xFF00000000000000 */
   udec_t mask = (dec_t)0xFF << (8 * (sizeof(dec_t) - 1));
   for(int byteNum = sizeof(dec_t)-1; byteNum >= 0; --byteNum)
   {
       dec_t byte = (decimal & mask) >> (8 * byteNum);
       printf("0x%.2X ", (int)byte);
       mask = mask >> 8;
       /* first time through, mask becomes 0x00FF000000000000 */
   }
   printf ("\n");
}

void printAsBinary(dec_t decimal)
{
   printf("%lld as binary: ", (long long)decimal);
   udec_t mask = (dec_t)1 << (8 * sizeof(dec_t) - 1);
   for(int bitNum = sizeof(dec_t) * 8 - 1; bitNum >= 0; --bitNum)
   {
       dec_t bit = (decimal & mask) >> bitNum;
       printf("%d", bit);
       mask = mask >> 1;
       if (bitNum % 8 == 0) printf(" ");
   }
   printf ("\n");
}

int main()
{
   dec_t decimal;

   while(1)
   {
       printf("Enter an integer value : ");
       scanf("%lld",&decimal);

       printAsHex(decimal);
       printAsBinary(decimal);


   }
   return 0;
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.