On 22 Jun, 04:28, Bartholomew Simpson <123evergr...@terrace.com>

wrote:

Slightly OT, but someone may know an algorithm to help me do this ....

I have six numbers that I want to encode into one single larger number.

The 6 numbers may be presented as ff:

number Range

num1 [0-9999.99]

num2 [0-9999.99]

num3 [0-9999.99]

num4 [0-9999.99]

num5 [0-9999.99]

num6 [0-16]

Can anyone help me with how to encrypt these numbers into one new

number: numEncoded

(and equally importantly), how to decode each of the components, when

presented with the encoded number ?

I know I can do this using prime numbers (co primes) - did it in

University a while back, but I can't seem to remember how to

encode/decode so I can write funcs like :

double encode(const double num1, const double num2,

const double num3, const double num4,

const double num5, const unsigned int num6);

and :

double GetNum1(const double encoded_number);

double GetNum2(const double encoded_number);

double GetNum3(const double encoded_number);

double GetNum4(const double encoded_number);

double GetNum5(const double encoded_number);

int GetNum6(const double encoded_number);

I don't know a clever way of doing this with priimes or anything, but

if the aim is to move from having 6 variables to having 1, I would

just convert them all to strings, left pad with spaces so they are all

(the first 5 at least) the same length and then concatenate them all

together.

I'd also be tempted to multiply the first 5 numbers by 100 to remove

having to work with decimals.

In an ideal world you could then convert this string to some kinfd of

integral numeric value. The problem is this (encoded) value might be

32 digits long, which is bigger than the largest standard integral

(ulong) can cope with.

If working with the string you do have the benefit of only having a

single variable; it is easy to extract the various component numbers;

plus there wouldn't be any clever maths going on that some future

developer might have to try and understand.

Your functions would have to look like this:

String encode(const double num1, const double num2, const double num3,

const double num4, const double num5, const unsigned int num6);

double GetNum1(const String encoded_number);

double GetNum2(const String encoded_number);

double GetNum3(const String encoded_number);

double GetNum4(const String encoded_number);

double GetNum5(const String encoded_number);

int GetNum6(const String encoded_number);

Depending on what you are doing, you could also look at combining the

6 numbers in to an object/record. This might be easier to work with.