Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hail monks - have any of you in your long years of wisdom come across an encoding for large numbers that works like this? (These are 8bit bytes)
here's 1:

64,8,0,0,0,0,0,0,0,240,63

here's 2:

64,8,0,0,0,0,0,0,0,0,64

and here's 131075:

64,8,0,0,0,0,0,24,0,0,65

All I can work out is that if you take the second byte from the right, a range of 16 for it will encode the range 2^n to 2^n+1. Where that range is bigger than 16 (most of the time), then the third byte to the right is called in, etc. - so 63 is:

64,8,0,0,0,0,0,0,128,79,64.

and 64:

64,8,0,0,0,0,0,0,0,80,64

that interval of 128 progressively halves until it's 1 (for 4096) and then a further byte is used,etc. If you've ever seen something like it please put me out of my misery...

Replies are listed 'Best First'.
Re: bizarre number encoding
by Dominus (Parson) on Dec 30, 2001 at 20:29 UTC
    That is double-precision IEEE floating point, with the bytes in backwards order.

    If you are using a little-endian machine, use

    my $n = unpack "d", pack "C*", @numbers;
    to recover the numeric value of the representation. (On a big-endian machine, use reverse @numbers.)

    Note that 64,8,0,0,0,0,0,0,0,240,63 is actually 1.00000000000047, not 1 exactly. You should double check and find out whether this extra .00000000000047 is really part of the number or not. It might be that the format is using four bytes for the numbers, with each one followed (or preceded) by another four bytes that mean something unrelated.

    Hope this helps.

    --
    Mark Dominus
    Perl Paraphernalia

Re: bizarre number encoding
by larsen (Parson) on Dec 30, 2001 at 19:45 UTC
Re: bizarre number encoding
by converter (Priest) on Dec 30, 2001 at 13:00 UTC

    It might be helpful to know the source of these data. The name of the application used to generate them, the type of the hardware platform, a vendor name, the medium from which they were sampled (disk, tape, network?) etc. might provide some useful clues.

Re: bizarre number encoding
by talexb (Chancellor) on Dec 30, 2001 at 09:17 UTC
    Based on the fact that it takes 8 bytes to encode each number, I'm guessing that the number is being stored as a floating point data type.

    But this forum is for Perl. What does this question have to do with Perl?

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.

      it's only perl-ish because I've been using unpack to parse these files.

      You're right, it is floating point - if you disregard the 64 and the following 2 bytes and unpack the rest as a double it comes out right.

      fyi this is a classic case of legacy systems, even though the application is only about 5 years old and Windows-based

      (you should see what they run the air traffic control system on. small mercies, it way predates windows...)

      thanks
        Hmmm...it seems like there are an insufficient number of examples to analyze. Can you provide examples for the numbers 0..15?

        If you were to convert your example for the number 2 into binary and reverse the order of the last byte you would get:

        00000010 which is decimal 2.

        Some food for thought.

        I would suggest looking for the code that writes/reads the number to disk and figuring out how it implements this encoding.

        metadoktor

        "The doktor is in."