Understanding Hexadecimal and Bytes in C #

It seems that I lack a fundamental understanding of computing and using hex values ​​and bytes in C # (or programming in general).

I would like to know how to calculate hexadecimal values ​​and bytes (0x--) from sources such as strings and RGB colors (for example, how to determine what the 0x code for R255 G0 B0 is?)

Why do we use things like FF, does the base system 10 compensate for a number like 10?

+4
source share
4 answers

Hexadecimal is base 16, so instead of counting from 0 to 9, we count from 0 to F. And we usually prefix hexadecimal constants with 0x . In this way,

 Hex Dec ------------- 0x00 = 0 0x09 = 9 0x0A = 10 0x0F = 15 0x10 = 16 0x200 = 512 

A byte is a typical storage unit for values ​​on a computer, and in most modern systems, bytes contain 8 bits. Note that bit actually means binary digit , so from this we get that the byte has a maximum value of 11111111 binary. These are 0xFF hex or 255 decimal places. Thus, one byte can be represented by at least two hexadecimal characters. A typical 4-byte int is then 8 hexadecimal characters, for example 0xDEADBEEF .

RGB values ​​are usually packed with 3 byte values ​​in that order, RGB. In this way,

 R=255 G=0 B=0 => R=0xFF G=0x00 B=0x00 => 0xFF0000 or #FF0000 (html) R=66 G=0 B=248 => R=0x42 G=0x00 B=0xF8 => 0x4200F8 or #4200F8 (html) 

For my hex calculations, I like to use python as my calculator:

 >>> a = 0x427FB >>> b = 700 >>> a + b 273079 >>> >>> hex(a + b) '0x42ab7' >>> >>> bin(a + b) '0b1000010101010110111' >>> 

For the RGB example, I can demonstrate how we could use a bit shift to easily calculate these values:

 >>> R=66 >>> G=0 >>> B=248 >>> >>> hex( R<<16 | G<<8 | B ) '0x4200f8' >>> 
+7
source

Base-16 notation (or hexadecimal) is convenient because you can put four bits in exactly one sixth digit, which makes converting to binary very simple, but not requiring as much space as a complete binary notation. This is useful when you need to present bit-oriented data in a readable form.

Learning hexadecimal is simple - all you have to do is remember a short table of 16 lines defining the hexadecimal 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 - 1010 D - 1011 E - 1110 F - 1111 

Using this table, you can easily convert hexadecimal strings of arbitrary length to the corresponding bit patterns:

 0x478FD105 - 01000111100011111011000100000101 

Converting back is also easy: group your binary digits into four and use the table to make hexadecimal digits

 0010 1001 0100 0101 0100 1111 0101 1100 - 0x29454F5C 
+2
source

In decimal value, each digit is weighed 10 times more than the one on the right, for example, β€œ3” in 32 is 3 * 10, and β€œ1” in 102 is 1 * 100. The binary is similar, except that there are only two digits ( 0 and 1), each bit is weighted twice as much as the one on the right. Hexadecimal uses 16 digits - 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 a binary number of n-digits can represent values ​​up to 2 ^ n-1.

Hexadecimal is convenient, as you can express one sixth digit in 4 bits, since 2 ^ 4 = 16 possible values ​​can be represented in 4 bits.

You can convert the binary code to hexadecimal by grouping from 4 bits to the right and converting each group to the corresponding hexadecimal. For example, 1011100 β†’ (101) (1100) β†’ 5C

Converting from hex to binary is even easier since you can simply expand every sixth digit into the corresponding binary code, for example 0xA4 β†’ 1010 0100

+1
source

The answer to the actual question ("Why do we use things like FF, does the base system 10 compensate for a number like 10?"): These are bits of computer use, that is, 1 or 0.

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

Binary numbers 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 format, you get the number 1010. This means that you need four bits to represent one 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 can also use numbers in base 16, not just base 10. That the hexadecimal number comes into play.


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

Converting between decimal and hexadecimal is more involved, and at least it was easier for me (if I have to do it in my head) to convert the decimal to binary first, and then the binary to hex. Google probably has many practices and algorithms for this.

+1
source

All Articles