Understanding hexadecimals and bytes in C#

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

    The answer to the actual question posted ("Why do we use things like FF, is it to compensate for the base 10 system to get a number like 10?") is this: Computer use bits, that means either 1 or 0.

    The essence is similar to what Lee posted and called "positional notation". In a decimal number, each position in the number refers to a power of 10. For example, in the number 123, the last position represents 10^0 -- the ones. The middle position represents 10^1 -- the tens. And the first is 10^2 -- the hundreds. So the number "123" represents 1 * 100 + 2 * 10 + 3 * 1 = 123.

    Numbers in binary use the same system. The number 10 (base 2) represents 1 * 2^1 + 0 * 2^0 = 2.

    If you want to express the decimal number 10 in binary, you get the number 1010. That means, you need four bits to represent a single decimal digit.

    But with four bits you can represent up to 16 different values, not just 10 different values. If you need four bits per digit, you might as well use numbers in the base 16 instead of only base 10. That's where hexadecimal comes into play.


    Regarding how to convert ARGB values; as been written in other replies, converting between binary and hexadecimal is comparatively easy (4 binary digits = 1 hex digit).

    Converting between decimal and hex is more involving and at least to me it's been easier (if i have to do it in my head) to first convert the decimal into binary representation, and then the binary number into hex. Google probably has tons of how-tos and algorithms for that.

提交回复
热议问题