views:

146

answers:

7

Hi every one!

I am programming on a project which I should store the key of the user to the initial configuration of a machine, I want to write it in C#.

I have an initial configuration which consists of two number R and X0, R = 3.9988 and X0 = 0.5. I want to add the user key to these numbers. for example:

Key: hos110 =>

  • R = 3.9988104111115049049048
  • X0 = 0.5104111115049049048

104111115049049048 are ASCII codes of the key which are concatenated.

How can I store these numbers?

Is there a better method for doing this?

Update: How about MATLAB?

+4  A: 

I don't really follow why you're using a key as part of a number, but leaving that aside... System.Decimal (aka decimal) seems like the right tool for the job here.

Jon Skeet
Hi, Thank you, I want to have arithmetic operation on it. I think by decimal users' keys is limited to 17 numbers for floating point.
Hossein Margani
A: 

It seems that you are storing a "key", so why not use a String then?

Groo
Hi, Thank you, I want to have arithmetic operation on it.
Hossein Margani
+1  A: 

Well, depending on the precision you are trying to achieve, you can probably save these as a pair of decimal values.

However, if this is an ASCII code, you may just want to save these as a string directly. This will avoid the numerical precision issues, especially if you're going to pull off the 104111... prior to using this information.

Reed Copsey
Hi, Thank you, I want to have arithmetic operation on it.
Hossein Margani
Why do you want to do arithmetic with a user key?
bdonlan
It's an initial configuration of a machine for generating a stream based on the user's key for encrypting an image.
Hossein Margani
I'd just use System.Decimal then. It gives you quite high-precision math, so you can do your arithmetic operations with less rounding error.
Reed Copsey
decimal data type precision is limited to 17.
Hossein Margani
@hossein Margani: That is not true. Decimals are not limited to 17 bits of precision, especially not when near the origin. For example, by default, decimal.ToString will preserve the first 28 bits of precision, which is enough for the strings you posted.
Reed Copsey
you mean my user key is limited to 9 characters? it's not very good.
Hossein Margani
If encryption is the goal why not use tried-and-tested .Net API? Rolling your own encryption rarely works out well (and you're likely not to know you failed). Is it steganography perhaps?
dbkk
Thanks, I am creating a new method for image encryption.
Hossein Margani
A: 

Floating point numbers are inherently imprecise. I'm not sure what this 'initial configuration' is or why it's a float, but you're not going to be able to tack on a 'user key' (whatever that may be) and recover it later. Store the user key separately, in a string or something.

bdonlan
A: 

If these 'numbers' have no numeric value, i.e. you will not use them for mathematical computation then there is no need to store them in a numeric datatype. You can store them as strings.

AdamRalph
+5  A: 

You're not really "adding" numbers. You are concatenating strings.

Store them as strings. You can't get much more precise than that.

If you need to perform any arithmetic operations, it is easy enough to convert them to a decimal number on the fly.

Robert Cartaino
Hi, Thank you, I want to have arithmetic operation on it.
Hossein Margani
+2  A: 

If you need infinite precision you need something that is called BigInteger. However these classes are usually only used for scientific calculations (and usually unsuited for stroring the data) which doesn't really seem to match your code sample. If you need to do only general calculations use Strings and then convert them to Decimal for the calculations.

However if you are looking for such a BigInterger Class you can find one here.

.Net 4.0 will have a BigInteger built-in-class in the class libraries named System.Numerics.BigInteger.

Foxfire
Thank you, does it have floating point?
Hossein Margani
Depends on which implementation you take. For the two mentioned ones you would have to keep track of the decimal place yourself.
Foxfire