next up previous FITS Documents
Next: Example Fortran 77 Code Up: Example C Code Previous: ASCII Encoding

ASCII Decoding

To invert the encoding, permute the string back to an alignment of zero bytes, subtract the hex 30 offset from each byte and pass the result to the checksum routine. A specific decoding routine is not required, but is provided as an example.

unsigned int char_decode (ascii)
char *ascii;                    /* assumed to be 16 bytes long */
{
        char cbuf[16];
        unsigned short *sbuf;
        unsigned int hi=0, lo=0, hicarry, locarry;
        int i;

        /* remove the permuted FITS byte
         * alignment and the ASCII 0 offset
         */
        for (i=0; i < 16; i++) {
            cbuf[i] = ascii[(i+1)%16];
            cbuf[i] -= 0x30;
        }

        sbuf = (unsigned short *) cbuf;

        for (i=0; i < 8; i+=2) {
            hi += sbuf[i];
            lo += sbuf[i+1];
        }
 
        hicarry = hi >> 16;
        locarry = lo >> 16;
        while (hicarry || locarry) {
            hi = (hi & 0xFFFF) + locarry;
            lo = (lo & 0xFFFF) + hicarry;
            hicarry = hi >> 16;
            locarry = lo >> 16;
        }

        return ((hi << 16) + lo);
}

If this routine is used to decode the value of the CHECKSUM keyword, the decoded result must be complemented to restore the original HDU checksum (including the contribution of the zero initialized CHECKSUM keyword value).