Understanding hexadecimals and bytes in C#

后端 未结 4 1950
执笔经年
执笔经年 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:08

    In decimal, each digit is weighted 10 times more than the one to the right, for example the '3' in 32 is 3 * 10, and the '1' in 102 is 1 * 100. Binary is similar except since there are only two digits (0 and 1) each bit is only weighted twice as much as the one to the right. Hexadecimal uses 16 digits - the 10 decimal digits along with the letters A = 10 to F = 15.

    An n-digit decimal number can represent values up to 10^n - 1 and similarly an n-digit binary number can represent values up to 2^n - 1.

    Hexadecimal is convenient since you can express a single hex digit in 4 bits since 2^4 = 16 possible values can be represented in 4 bits.

    You can convert binary to hex by grouping from the right 4 bits at a time and converting each group to the corresponding hex. For example 1011100 -> (101)(1100) -> 5C

    The conversion from hex to binary is even simpler since you can simply expand each hex digit into the corresponding binary, for example 0xA4 -> 1010 0100

提交回复
热议问题