I have problem with getting value from binary registry key. My code so far is

string key = "UserPreferencesMask";
        string key_place = @"Control Panel\Desktop";
        RegistryKey root = Registry.CurrentUser;
        root = root.OpenSubKey(key_place);
        var byte_value = root.GetValue(key);
        byte[] bytearray = byte_value as byte[];
        //How can convert byte [] array to 64 bit binary string
         klase.Close();
         MessageBox.Show(binary_string);

I am stacked when i want

UserPreferencesMask

binary registry key to convert 64bit binary string . something like this from

9E3E078012000000

hex value to

1001111000111110000001111000000000010010000000000000000000000000

binary value

But if i build code which convert from hex to binary, i have wrong binary value if in hex number has first one or two or more zero numbers. Binary returns without first zero numbers, only count start from first byte value with 1 , and has no error if hex started with any other char,except 0 How can i solve problem ? I searched everywhere, but i found only hex to binary ., but this cause an error for me, and returned value aren't 64 bit value lenght. Thank's !

share|improve this question

62% accept rate
feedback

3 Answers

up vote 0 down vote accepted
Converter.ToString( BitConverter.ToInt64( bytearray, 0 ), 2 ).PadLeft( 64, '0' ); 

If the original number is the wrong endianness then its pretty easy to just do an:

Array.Reverse( bytearray );

before you run the first line.

share|improve this answer
feedback

This should work:

string binary_string = "";
for (int i = 0; i < byte_value.Length; i++)
{
    binary_string += Convert.ToString(byte_value[i], 2).PadLeft(8, '0');
}

Unless byte_value in little-endian, then the loop has to be reversed.

share|improve this answer
feedback

You can convert an integer to a hexadecimal value by specifying a format

long x = 1207703453;
string hex = x.ToString("X16"); // "0000000047FC179D" 

This will always produce a hex value with 16 digits.

From this it is easy to produce a binary representation. I suggest using a dictionary.

var hexToBinDict = new Dictionary<char, string> { 
    { '0',"0000"},
    { '1',"0001"},
    { '2',"0010"},
    { '3',"0011"},
    { '4',"0100"},
    { '5',"0101"},
    { '6',"0110"},
    { '7',"0111"},
    { '8',"1000"},
    { '9',"1001"},
    { 'A',"1010"},
    { 'B',"1011"},
    { 'C',"1100"},
    { 'D',"1101"},
    { 'E',"1110"},
    { 'F',"1111"}
};

var sb = new StringBuilder(64);
foreach (char hexdigit in hex) {
    sb.Append(hexToBinDict[hexdigit]);
}
string result = sb.ToString(); 
// "0000000000000000000000000000000001000111111111000001011110011101"
share|improve this answer
1  
But he wants a binary number ... doesn't he? – Goz Aug 9 at 19:45
If I understand his question right, he tries to convert the number to a hexadecimal representation first and then converts this one to a binary one (maybe by using a conversion table). His problem is that the hex representation is missing leading zeroes. – Olivier Jacot-Descombes Aug 9 at 19:50
I don't think he does ... but equally you don't suggest a method for converting a hex value into a binary value ... You'd end up being forced to either step through each character and generate the bits (admittedly only requires a 16 entry lookup table) or you'd convert back from a string and use Convert.ToString( ... ) anyway ... – Goz Aug 9 at 19:53
I added a suggestion for a full conversion to binary. – Olivier Jacot-Descombes Aug 9 at 20:08
whoaa...anywhay big thanks ! – user1562839 Aug 9 at 21:20
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.