C# Reading registry key binary and converting to string(char)

fsX

Member
Joined
Jul 8, 2004
Messages
20
Hello,
Having a tough time reading a binary registry value and converting to string char value:

byte[] bytes = null;
try
{
bytes = (byte[])tempkey.GetValue(valueName);
for (int i=0; i<bytes.Length; i++)
stringbytes = (stringbytes + bytes);
}

I get this - 65048050000
The regkey has this - 41 00 30 00 32 00 00 00 A 0 2
I need this - A 0 2 (as A02) and also the hex values.

:confused:
Can anyone please help...
Thx fsX
 
fsX said:
Hello,
I get this - 65048050000
The regkey has this - 41 00 30 00 32 00 00 00 A 0 2
I need this - A 0 2 (as A02) and also the hex values.
Let me translate the reg key for you to see if you can see where your going wrong:

41 = 0x41 = 65 - in other words the first byte = 41 which is hexadecimal for decimal 65 which is character A

00 = 0x0 = 0 which is the null character; empty space - it is ignored in your output when your doing it right.

30 = 0x30 = 48 - in other words the third byte = 30 which is hexadecimal for decimal 48 which is character 0

00 = 0x0 = 0 which is the null character; empty space - it is ignored in your output when your doing it right.

32 = 0x32 = 50 - in other words the fifth byte = 32 which is hexadecial for decimal 50 which is character 2

this would give you the A02 your looking for.

You get the output of 6505805000 because each byte is converted to string which is just its value. Its working exactly like you told it to.

What you want is
stringbytes += char[bytes];

or something close to that - cant remember the exact procedure to get the value character out and I dont a VS on me right now.

Knowing the diferance between Hex (0-F), and Decimal (0-9), how to convert them, and having a ASCII character code chart to compare it all for letters is essential - get yourself one. Also study up on binary. That wasnt binary data you were looking at. That was a hexadecimal; its very easy to convert hex to binary though, that is why computers use it, it is displayed in hexadecimal to make it easier to read as you will see below.

Your value:
41 00 30 00 32 00 00 00

in true binary wouldve been:
00101001 00000000 00011110 00000000 00100000 00000000 00000000 00000000
 
Last edited by a moderator:
Thanks for your input bri189a; your post really helps! :-\
I will give it a try.

fsX
 
fsX said:
Still cant get it right! :confused: :confused: :confused:
This just doesnt work...
stringbytes += (char) bytes;

That will work. I made one that works that uses a foreach instead - I just couldnt remember the syntax:

RegistryKey r = Registry.LocalMachine.OpenSubKey("HARDWARE").OpenSubKey("ACPI").OpenSubKey("DSDT").OpenSubKey("SONY").OpenSubKey("C0").OpenSubKey("20011122");
byte [] val = (byte[])r.GetValue("00000000");
string output = "";
foreach(byte b in val)
{
output += (char) b;
}
Console.WriteLine(output);
Console.ReadLine();
 
Back
Top