By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,179 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,179 IT Pros & Developers. It's quick & easy.

encoding/decoding question

P: n/a
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);

Jun 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Thu, 21 Jun 2007 20:28:50 -0700, Bartholomew Simpson
<12**********@terrace.comwrote:
[...]
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
Jun 22 '07 #2

P: n/a
Bartholomew Simpson <12**********@terrace.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 22 '07 #3

P: n/a
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.

Jun 22 '07 #4

P: n/a


Jon Skeet [C# MVP] wrote:
Bartholomew Simpson <12**********@terrace.comwrote:
>>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 ?
>
Jun 22 '07 #5

P: n/a
On Jun 22, 11:19 am, Bartholomew Simpson <123evergr...@terrace.com>
wrote:
>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.
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
Again, take "significant digits" instead.
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 ?
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.
> 12 bits is only enough to give 3-4 decimal places of accuracy.
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

Jun 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.