C# Dictionary hex, bin

Markus Freitag 3,786 Reputation points
2021-01-12T12:26:27.147+00:00

Hello,

Code:
HEX: FF70000000000000000000000
BIN: 1111 1111 0111 -> sample pos 8 is bad
Dez:

I am looking for a way where I can convert a hex value to a dictionary with 0 or 1. What is the best way to do this?

Dictionary dictGoodBad;
INPUT (string as hex) --> OUTPUT Dictionary or list.

Key Value
0 true
1 true
2 true
3 true

...

8 false
9

55669-bin-hex.png

or other direction.
Thanks for help!
55696-bin-hex-other-direction.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-01-13T01:46:26.2+00:00

    Try using linq, it should meet your needs:

                string hexString = @"FF70000000000000000000000";  
                string binString  = String.Join(String.Empty, hexString.Select(c => Convert.ToString(Convert.ToUInt32(c.ToString(), 16), 2).PadLeft(4, '0')));  
      
                List<bool> result =  binString.Select(c => c == '1').ToList();  
    

    55873-1.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2021-01-13T01:33:40.163+00:00

    I;m not sure on which part of the task you want advice. If you just want
    to store the key (bit position) and value (true or false) in a Dictionary
    based on the values in a binary string, then this should work:

    binstr = "11111111111111110111";
    
    Console.WriteLine("\nBinary string: {0}", binstr);
    Dictionary <int, bool> bitDict = new Dictionary<int, bool>();
    int count = 0;
    foreach(char bitchar in binstr)
    {
        bitDict.Add(count++, bitchar == '1' ? true : false);
    }
    foreach(KeyValuePair<int, bool> kp in bitDict)
    {
        Console.WriteLine("{0} {1}", kp.Key, kp.Value);
    }
    

    Output:

    Binary string: 11111111111111110111
    0 True
    1 True
    2 True
    3 True
    4 True
    5 True
    6 True
    7 True
    8 True
    9 True
    10 True
    11 True
    12 True
    13 True
    14 True
    15 True
    16 False
    17 True
    18 True
    19 True

    I'm assuming that bit number ordering is not important in this case.

    LSB->MSB or MSB->LSB

    If you're looking for the whole enchilada - converting a hex string to
    a binary string and then populate the Dictionary, then you may be asking
    us to write the whole code for you. There are many examples of converting
    a hex string on the web, so you should be able to do that part after
    a little online research.

    • Wayne
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.