如何在 Windows 注册表中存储双精度(64 位浮点)或浮点数?

发布于 2024-09-30 13:31:39 字数 200 浏览 0 评论 0原文

我想将 double (64 位浮点)存储到 Window 注册表中。最好的方法是什么?

可能的选项:64 位编译器中的 longdouble 都是 64 位值,因此一种选择是将 double 转换为其 64 位表示形式并存储它作为注册表“qword”。另一种选择是将其存储为二进制流。

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: both 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光的暖冬 2024-10-07 13:31:39

或者您可以将其存储为 base64 编码的字符串或其他形式。 :)

就我个人而言,我会将其存储为 reg_binary,而不是来回转换,但哪个最简单实际上取决于语言。至于哪一个“更正确”,我会说 reg_binary。但注册表是一个肮脏的黑客行为,如果更容易的话,我不会因为以“错误”的方式做一些事情而感到难过。

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.

很酷不放纵 2024-10-07 13:31:39

您也可以使用字符串。

例如

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

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"));
無心 2024-10-07 13:31:39

我在某处看到的一个聪明的技巧是他们将分子和分母存储为单独的 DWORD,然后在需要时将它们组合起来。

one clever trick I saw somewhere was they would store the numerator and denominoator as separate DWORD's, then combine them when needed.

花间憩 2024-10-07 13:31:39

您可以在此页面。 您应该看一下:

  1. REG_SZ - 如果您使用它,您可以将双数存储为字符串。
  2. REG_QWORD - 您可以将双精度存储为双精度。
  3. REG_BINARY - 您可以将双精度存储为二进制数据。

第二个可能看起来最好,但是在这种情况下我使用第三个选项。 floutdouble 类型基于 IEEE754 类型,因此您可以轻松地将数字分解为 64 位,并且不会丢失任何精度(与 代码>REG_SZ)。

You can find all Windows registry types in this page. You should take a look at:

  1. REG_SZ - if you use this you can store your double number as a string.
  2. REG_QWORD - you can store your double as a double.
  3. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文