# [ACCEPTED]-Understanding hexadecimals and bytes in C#-byte

Hexadecimal is base 16, so instead of counting 16 from 0 to 9, we count from 0 to F. And 15 we generally prefix hex constants with `0x`

. Thus,

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

A 14 byte is the typical unit of storage for 13 values on a computer, and on most all modern 12 systems, a byte contains 8 bits. Note that 11 `bit`

actually means `binary digit`

, so from this, we gather 10 that a byte has a maximum value of 11111111 9 binary. That is 0xFF hex, or 255 decimal. Thus, one 8 byte can be represented by a minimum of 7 two hexadecimal characters. A typical 4-byte 6 `int`

is then 8 hex characters, like `0xDEADBEEF`

.

RGB values 5 are typically packed with 3 byte values, in 4 that order, RGB. Thus,

```
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 3 like to use python as my calculator:

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

For 2 the RGB example, I can demonstrate how we 1 could use bit-shifting to easily calculate those values:

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

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

Learning hex 8 is easy - all you need to do is memorizing 7 a short table of 16 rows defining hex-to-binary 6 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 5 can easily convert hex strings of arbitrary 4 length to their corresponding bit patterns:

```
0x478FD105 - 01000111100011111011000100000101
```

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

```
0010 1001 0100 0101 0100 1111 0101 1100 - 0x29454F5C
```

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

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

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

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

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

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

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

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

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

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

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

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

More Related questions

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.