[comp.lang.c] BCD Encoding?

wew@naucse.UUCP (Bill Wilson) (04/10/90)

dBASE IV writes it's new numeric variables to the memory variable save
files in BCD format unless the variable is set up as fixed.  I need
to know the format of BCD so that I can interpret the numbers for
a project that I am working on.  Any clues?


-- 
Let sleeping dragons lie........               | The RoleMancer 
----------------------------------------------------------------
Bill Wilson             (Bitnet: ucc2wew@nauvm | wilson@nauvax)
Northern AZ Univ  Flagstaff, AZ 86011

ts@uwasa.fi (Timo Salmi LASK) (04/11/90)

In article <2024@naucse.UUCP> wew@naucse.UUCP (Bill Wilson) writes:
>dBASE IV writes it's new numeric variables to the memory variable save
>files in BCD format unless the variable is set up as fixed.  I need
>to know the format of BCD so that I can interpret the numbers for
>a project that I am working on.  Any clues?

You might try to take a look at Turbo Pascal version *3* manual, or
Turbo Professional 5.0 (from TurboPower) manual. They both include
BCD arithetic, and might have the specs.

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

rspangle@jarthur.Claremont.EDU (Randy Spangler) (04/11/90)

Microsoft Macro Assembler 5.2 manual gives the following:

Packed BCD numbers are stored 2 digits to a byte, with one digit in the 
lower 4 bits and one in the upper 4 bits

Unpacked BCD numbers are stored one digit to a byte, in the last 4 bits.
The sign is in the leftmost bit (0 is +, 1 is -).


-- 
 --------------------------------------------------------------------------
|    Randy Spangler                    |    The less things change, the    |
|    rspangle@jarthur.claremont.edu    |    more they remain the same      |
 --------------------------------------------------------------------------

awd@dbase.A-T.COM (Alastair Dallas) (04/12/90)

In article <2024@naucse.UUCP>, wew@naucse.UUCP (Bill Wilson) writes:
> dBASE IV writes it's new numeric variables to the memory variable save
> files in BCD format unless the variable is set up as fixed.  I need
> to know the format of BCD so that I can interpret the numbers for
> a project that I am working on.  Any clues?

The information is proprietary, but 1) even _I_ would vote to release it,
I think (and I'm pretty conservative); and 2) Ashton-Tate is planning to
release file format information in some form to some group of people
(developers under non-disclosure, everyone, or something inbetween) soon.
I will make it my business to find out more about our policy and respond
to the net.

/alastair/

hughesl@jacobs.CS.ORST.EDU (Larry Hughes) (04/12/90)

Ah, the wonderful world of BCD.  Actually, in the back of the "Programmer's
Guide to the IBM PC by Peter Norton, there is a pretty good discussion of
BCD formats - apparently there are more than one.  I have tried, rather un-
successfully, to break out one BCD format done by a program called MAGIC,
so if anyone out there can offer suggestions, please respond by Email.  There
are binary to ascii conversion functions in most languages, these days, but
dealing with the old BASIC format in PASCAL can be a problem.

Other than that, I am afraid I could use some more info on the subject.

l.e.h.
 

wew@naucse.UUCP (Bill Wilson) (04/13/90)

From article <522@dbase.A-T.COM>, by awd@dbase.A-T.COM (Alastair Dallas):
> The information is proprietary, but 1) even _I_ would vote to release it,
> I think (and I'm pretty conservative); and 2) Ashton-Tate is planning to
> release file format information in some form to some group of people
> (developers under non-disclosure, everyone, or something inbetween) soon.
> I will make it my business to find out more about our policy and respond
> to the net.
>
Well guess what?  I figured it out by myself.  Here it is in case
anyone else needs to do it.  You will need a hex dumper to get it 
perfect.

dBASE IV now has two numeric data types: fixed (BCD) and float.  There
are two forms to the fixed numbers in memory variable files: integer
and fixed decimal.  The integer uses a traight nibble=number encoding
that follows the standards that I have been made aware of through
helpful people on the net.  The second form is more complex in some
ways and just as simple in others.  The following is a description
of the format:

Field    N Bytes    Description
1          11        Variable name
2           1        Variable type
3           1        Var sub type
4          19        not sure what this area is for...
5           1        variable size/info
6           1        sign
7          10        BCD number

Field number five indicates the size of the variable.  It is based
on the number 34h.  If the size is greater than 34h, subtract 34h
from the number and that is the number of whole digits from the
beginning of the BCD number.  If the number is less than 34h then
the differnce is the number of extra zeros to put after the implied
decimal point and before the BCD number.  For example, if you have 
the hex bytes:
   36 0d 20 50
36h-34h=2 which means you take the first two nibbles as the whole
portion and the rest as the decimal portion.  So the number would
be 20.50  Simple Huh?

If the number was negative the 0d would be 8d.  The first nibble 
indicates the sign.

The following is a list of the variable type codes as listed in field
two:
c3 - string
ce - float (IEEE format)
cc - logical
c4 - date
c6 - BCD numeric (fixed)

Some articles were published in Data Based Advisor (V5 N4, N5) that
helped in decoding the memory variable files.  If anyone else figures 
anything else out on these files, please let me know.


-- 
Let sleeping dragons lie........               | The RoleMancer 
----------------------------------------------------------------
Bill Wilson             (Bitnet: ucc2wew@nauvm | wilson@nauvax)
Northern AZ Univ  Flagstaff, AZ 86011

awd@dbase.A-T.COM (Alastair Dallas) (04/14/90)

BCD numbers, in general, are "packed"--that is, two decimal digits are stored
in a byte.  A hex dump of the number 123 might look like:

		12 30      

(Note that a hex dump is as good as a decimal BCD dump.)
Other information stored with each number is the sign (+/-), the exponent
(so you know where the decimal point goes--some implementations are fixed
point, however, and don't need this) and the exponent and the sign of the
exponent.

Hope that helps--I'm just not at liberty to describe our .mem file format.
BCD numbers are not proprietary, but there are a lot of different formats
for BCD and I can't discuss specific nuances.

/alastair/

Disclaimer> No corporate, just programmer-a-programmer :-)