Understanding hexadecimals and bytes in C#

后端 未结 4 1951
执笔经年
执笔经年 2021-01-31 06:39

I seem to lack a fundemental understanding of calculating and using hex and byte values in C# (or programming in general).

I\'d like to know how to calculate hex values

4条回答
  •  借酒劲吻你
    2021-01-31 07:20

    Base-16 (also known as hex) notation is convenient because you can fit four bits in exactly one hex digit, making conversion to binary very easy, yet not requiring as much space as a full binary notation. This is useful when you need to represent bit-oriented data in a human-readable form.

    Learning hex is easy - all you need to do is memorizing a short table of 16 rows defining hex-to-binary conversion:

    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
    

    With this table in hand, you can easily convert hex strings of arbitrary length to their corresponding bit patterns:

    0x478FD105 - 01000111100011111011000100000101 
    

    Converting back is easy as well: group your binary digits by four, and use the table to make hex digits

    0010 1001 0100 0101 0100 1111 0101 1100 - 0x29454F5C
    

提交回复
热议问题