简体   繁体   English

C# 二进制数组“{ 84, 01, 00, 00 }” 在 Windows 注册表中更改为“54 01 00 00”

[英]C# Binary Array “{ 84, 01, 00, 00 }” changed to “54 01 00 00” in Windows Registry

I'm using C# to write the following binary value to the system registry:我正在使用 C# 将以下二进制值写入系统注册表:

  byte[] valueToSet = { 84, 01, 00, 00 };
  Registry.SetValue(keyName, "00036601", valueToSet);
  // returns void as expected.

When I view the written value using regedit, I see the displayed value as 54 01 00 00. This is incorrect当我使用 regedit 查看写入的值时,我看到显示的值为 54 01 00 00。这是不正确的

Conversely when I set the value to 84,01,00,00 manually in the registry, and run this code相反,当我在注册表中手动将值设置为 84,01,00,00 并运行此代码时

object ttt = Registry.GetValue(keyName, "00036601", null);
// returns 132, 1, 0, 0

Is this an Endian issue?这是一个Endian问题吗? What is the correct way to fix this?解决此问题的正确方法是什么?

It's a hexadecimal issue.这是一个十六进制问题。 RegEdit displays values in hexadecimal, and you're working with decimal in C#. RegEdit 以十六进制显示值,而您在 C# 中使用十进制。

The two values are equivalent:这两个值是等价的:

0x54 == 84

The registry editor displays the value in hexadecimal.注册表编辑器以十六进制显示该值。 The decimal value 84 is equal to the hexadecimal value 0x54, and hexadecimal 0x84 is equal to decimal 132.十进制值 84 等于十六进制值 0x54,十六进制 0x84 等于十进制 132。

I think you are trying to specify the literals in hex .我认为您正在尝试以hex指定文字。 However, without the 0x hex-prefix, the compiler interprets them as decimals .但是,如果没有0x十六进制前缀,编译器会将它们解释为decimals

Try this instead:试试这个:

byte[] valueToSet = { 0x84, 0x01, 0x00, 0x00 };

The values shown in the registry for binary types are in hexidecimal notation.二进制类型的注册表中显示的值采用十六进制表示法。 Decimal 84 == hex 0x54.十进制 84 == 十六进制 0x54。

interesting thing to note is that 84 (decimal) == 54 (hex)有趣的是, 84 (十进制)== 54 (十六进制)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM