How do I store a double (64-bit floating point) or float in the Windows registry?
I want to store a double
(64-bit floating point) into the Window registry. What's the best way to go about it?
Possible options: bot开发者_JS百科h a long
and a double
in a 64-bit compiler are 64-bit values so one option is to convert the double to it's 64-bit representation and store it as a registry "qword". Another option is to store it as a binary stream.
Or you could store it as a base64 encoded string, or whatever. :)
Personally though, I'd store it as a reg_binary rather than converting to and fro, but which ever is easiest really depends on the language. As far as which one is "more right" I'd say reg_binary. But the registry is such a dirty hack that I wouldn't feel bad doing something the "wrong" way if it's easier.
You could use a string too.
e.g.
var software = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
var mycompany = software.CreateSubKey("MyCompany");
var emailagent = mycompany.CreateSubKey("EmailAgent");
emailagent.SetValue("SendMailPollingTime", "1000", RegistryValueKind.String);
emailagent = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\MyCompany\\EmailAgent");
double polling = double.Parse((string)emailagent.GetValue("SendMailPollingTime"));
one clever trick I saw somewhere was they would store the numerator and denominoator as separate DWORD's, then combine them when needed.
You can find all Windows registry types
in this page. You should take a look at:
- REG_SZ - if you use this you can store your double number as a string.
- REG_QWORD - you can store your double as a double.
- REG_BINARY - you can store your double as a binary data.
The second probably looks best, however, in such cases I use third option. flout
and double
types are based IEEE754
type, so you can easy explode your number into 64 bits and do not lose any precision (compared with REG_SZ
).
精彩评论