encoding/decoding question

  • Thread starter Bartholomew Simpson
  • Start date
B

Bartholomew Simpson

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);
 
P

Peter Duniho

[...]
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 ?

Well, .NET has the Cryptography namespace that you can use to encrypt
stuff. So I would just put your data into some kind of single data
structure (serialize it into a MemoryStream, for example) and then encrypt
it. Decrypt it later, and deserialize it, and there you go.

I realize that's a more general reply than the specific "into one new
number", but since you don't specify just how large the "new number" is
expected to be, and the example input data isn't going to fit in 64 bits
(the longest integral numeric type I'm aware of), I figure you're not
really wanting to stick everything literally into a single number.

Pete
 
J

Jon Skeet [C# MVP]

Bartholomew Simpson said:
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]

Do you need 6 decimal places stored accurately for each of the first
five numbers? That's 30 decimal places to start with, which is more
than any of the built-in numeric types in .NET can cope with.

In particular, your required method returns double, which is only 64
bits - that gives you about 12 bits for each of the first five numbers
(even assuming they can be evenly distributed, which they can't
really). 12 bits is only enough to give 3-4 decimal places of accuracy.
 
B

Bartholomew Simpson

Jon said:
Bartholomew Simpson said:
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]


Do you need 6 decimal places stored accurately for each of the first
five numbers?

Erm, no - the numbers above, have only 2 dp (the most I would need would
be 3 dp)

That's 30 decimal places to start with, which is more
than any of the built-in numeric types in .NET can cope with.

No, assuming I go for the highest resolution (3dp) - its 3x5 = 15
In particular, your required method returns double, which is only 64
bits - that gives you about 12 bits for each of the first five numbers
(even assuming they can be evenly distributed, which they can't
really).

What does that mean "evenly distibuted"? - are you taking about a
mathematical distribution function ?

12 bits is only enough to give 3-4 decimal places of accuracy.

Useful piece of information - this may help me with choosing dp and an
encoding alo - do you have a reference to the link floating pt
representation and nuumber of bits ?
 
M

Matt Lacey

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.
 
J

Jon Skeet [C# MVP]

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]
Do you need 6 decimal places stored accurately for each of the first
five numbers?

Erm, no - the numbers above, have only 2 dp (the most I would need would
be 3 dp)

Whoops, sorry - I meant significant digits, not decimal places.
No, assuming I go for the highest resolution (3dp) - its 3x5 = 15

Again, take "significant digits" instead.
What does that mean "evenly distibuted"? - are you taking about a
mathematical distribution function ?

I mean in terms of distributing the available bits evenly between the
different numbers you want to represent. In practice it's tricker
because there's the mantissa and the exponent separately.

You'd probably be better off storing the whole thing as a long (or a
decimal which happened to be a whole number) and working out your own
floating point system within that.

And again :)
Useful piece of information - this may help me with choosing dp and an
encoding alo - do you have a reference to the link floating pt
representation and nuumber of bits ?

See http://pobox.com/~skeet/csharp/floatingpoint.html

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top