Click here to Skip to main content
16,015,414 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I have a program that list the locations of License in 1 combobox and list the names of the Values in another Combobox.
I then select a name in the second, then hit a button to return the data in the key.
It is Regbinary.
Only problem is What is returned is not the hex format like viewing thru regedit but it is returning decimal.

I would like to output the hex into a textbox also.

Would Anyone tell me why this code would return Decimal instead of hex, or even binary ?
I have verified the Code works by Converting each byte seen in regedit to the Result Displayed in my program.
Thanks for your Time.
Try
    Dim strbldr As New StringBuilder

    Dim regbinary() As Byte
    Dim LicenseNumber As String
    LicenseNumber = ComboBoxKeyValueName.SelectedItem.ToString

    If ComboBoxLicLocation.SelectedItem = LicKey1 Then
        SelectedRegLocation = LicKey1.ToString
        regbinary = (Registry.LocalMachine.OpenSubKey( _
                  "SOFTWARE\Wow6432Node\Licenses").GetValue(LicenseNumber))

    ElseIf ComboBoxLicLocation.SelectedItem = LicKey2 Then
        SelectedRegLocation = LicKey2.ToString
        regbinary = (Registry.LocalMachine.OpenSubKey( _
                   "SOFTWARE\Licenses").GetValue(LicenseNumber))
    ElseIf ComboBoxLicLocation.SelectedItem = LicKey3 Then
        SelectedRegLocation = LicKey3.ToString
        regbinary = (Registry.CurrentUser.OpenSubKey( _
                   "Software\Licenses").GetValue(LicenseNumber))
    End If



    For Each strbyte In regbinary
        strbldr.Append(strbyte & ",")


    Next
    txtbxOutputDecimalArray.Text = strbldr.ToString
    lblCount.Text = "Number of bytes : " & regbinary.Count.ToString

    Registry.LocalMachine.Close()
Catch ex As Exception

End Try
Posted
Updated 9-Jan-12 20:00pm
v3
Comments
Sergey Alexandrovich Kryukov 9-Jan-12 22:16pm    
First of all, change the tag in the question. It's VB.NET, not VB, a very different language.
--SA
Sergey Alexandrovich Kryukov 9-Jan-12 22:36pm    
"Does anyone know?" looks nearly offensive. Who do you think most of our members are?
--SA
Sergey Alexandrovich Kryukov 9-Jan-12 22:37pm    
Who told you that this code "returns decimal"? In case of such a fundamental confusion, I feel it hard to explain... I'll try...
--SA
ledtech3 10-Jan-12 2:00am    
1. Updated tag.
2. Changed Question.
3. Updated how I verified the Result.
4. My education mostly stems from what I can find on the internet,Learn here,and MSDN.I Do not work in a shop with other coders and have no one to ask.My books are from 2005 and all seem to have the same Code samples in them.

Look at the description of GetValue: http://msdn.microsoft.com/en-us/library/fdf576x1.aspx[^].

The return type if System.Object. This is a compile-time type of the returned object, and the concrete run-time type depends on the key. You can learn what type this value should be cast to by calling GetValueKind, http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.getvaluekind.aspx[^].

You always need to validate the value type.
You don't do it; and this is your big mistake.

Now, if the returned value is really numeric or binary (as your code suggests), it simply cannot be decimal or hexadecimal — it's binary. If you don't understand it, you should start your computer education pretty much from the very beginning. You problem is not that you don't know how to show the data correctly, your problem is that your question makes no sense as it is based on misunderstanding of data representation, as far as I can see from your question — any data.

Decimal or hexadecimal is only related to string representation of binary data (any numeric type is also binary). You can present it in a string in many different ways, but the data remains the same. Appending bytes to StringBuilder make little sense. You can present your binary data in the form of array of bytes in many different ways and different order; all of them are equally correct but should come with some "legend". A hint: use string.Format, see:

http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA
 
Share this answer
 
v2
Comments
ledtech3 10-Jan-12 10:21am    
I added a text box, and added the GetValueKind, and the return Value was 3.
If you Go by the list on Page http://msdn.microsoft.com/en-us/library/microsoft.win32.registryvaluekind(v=VS.90).aspx
Then the Third Item Is Binary,(can't Find a chart that says what the returned value means) which I already knew by viewing thru regedit.
I used the stringbuilder because I could find no usable code samples for working with Array of bytes from a Regbinary registry key,and that was what I was able to get to work.
Looking at your links Again.
Thanks again for your time.
Sergey Alexandrovich Kryukov 11-Jan-12 1:06am    
You are welcome. Main thing is understanding how data is presented in a computer, not formatting strings which is just a matter of... well, routine string manipulation.
Hope you will see it makes sense; consider accepting the answer formally (green button) -- thanks.
--SA
ledtech3 11-Jan-12 11:38am    
I marked Both answers as accepted because your answer, Did answer the question I did ask, and my post answered the question I should have asked.I gave your's a 4 because I still had to decipher msdn's code to get it to work.
Have you ever felt like your running in circles trying to track stuff down on that site ?
And thanks again for your help.
Sergey Alexandrovich Kryukov 11-Jan-12 13:25pm    
Hm. "I gave your's a 4 because I still had to decipher msdn's code to get it to work". Is this because you prefer digest to original source? :-)

OK, for your pleasure:

byte value = //...;
string hexadecimal = value.ToString("X"); //this "X" will do the trick

Better now? :-)

Good luck, call again.
--SA
ledtech3 14-Jan-12 20:44pm    
Just seen your reply, Didn't get the email notice for some reason.
Thanks for the code sample.
The Learning experience helped me get better insight with another project.
I have only voted once before and hit the wrong star(the low end) and once you make a mistake you can not fix it.So at least I was on the right end this time.
Using the Idea from the code on the page here
http://msdn.microsoft.com/en-us/library/microsoft.win32.registryvaluekind(v=VS.90).aspx[^]

I was able to create another test project. 1 textbox and 5 buttons.
By Working with the Format I could change the way the output looked, by adding spaces or the comma to "{0:X2}," .I still had to use the Stringbuilder Because I don't know of another way to get the array into a textbox without building the string.
The result returned is the same that shows up in Regedit.
See the code below, the output is commented in the button click event it was created in.

Thanks again for pushing me in the right direction.

Note: Watch out for line wrapping in the posted code.

Imports System
Imports Microsoft.Win32
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strbldr As New Stringbuilder
        Dim regbinary() As Byte
        regbinary = (Registry.CurrentUser.OpenSubKey("Software\Licenses").GetValue("{R7C0DB872A3F777C0}"))

        For Each strbyte In regbinary
            strbldr.AppendFormat("{0:X2} ", strbyte)
        Next 'returns   2F 59 41 42

        TextBox1.Text = strbldr.ToString
      
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim strbldr As New StringBuilder
        Dim regbinary() As Byte
        regbinary = (Registry.CurrentUser.OpenSubKey("Software\Licenses").GetValue("{R7C0DB872A3F777C0}"))

        For Each strbyte In regbinary
            strbldr.AppendFormat("{0:X2}", strbyte)
        Next  'Returns   2F594142

        TextBox1.Text = strbldr.ToString
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim strbldr As New StringBuilder
        Dim regbinary() As Byte
        regbinary = (Registry.CurrentUser.OpenSubKey("Software\Licenses").GetValue("{R7C0DB872A3F777C0}"))

        For Each strbyte In regbinary
            strbldr.AppendFormat("{0:X2} ", strbyte & ",")
        Next  'Returns    47, 89, 65, 66,

        TextBox1.Text = strbldr.ToString
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim strbldr As New StringBuilder
        Dim regbinary() As Byte
        regbinary = (Registry.CurrentUser.OpenSubKey("Software\Licenses").GetValue("{R7C0DB872A3F777C0}"))

        For Each strbyte In regbinary
            strbldr.AppendFormat("{0:X2},", strbyte)
        Next   'Returns   2F,59,41,42,

        TextBox1.Text = strbldr.ToString
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim strbldr As New StringBuilder
        Dim regbinary() As Byte

        regbinary = (Registry.CurrentUser.OpenSubKey("Software\Licenses").GetValue("{R7C0DB872A3F777C0}"))
        For i As Integer = 0 To regbinary.Length - 1
            ' Display each byte as two hexadecimal digits.
            strbldr.AppendFormat("{0:X2}", regbinary(i))
        Next  'Returns   2F594142

        TextBox1.Text = strbldr.ToString
       End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900