了解 C# 中的十六进制和字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12081523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding hexadecimals and bytes in C#
提问by Daniel Minnaar
I seem to lack a fundemental understanding of calculating and using hex and byte values in C# (or programming in general).
我似乎对在 C#(或一般编程)中计算和使用十六进制和字节值缺乏基本的了解。
I'd like to know how to calculate hex values and bytes (0x--) from sources such as strings and RGB colors (like how do I figure out what the 0x code is for R255 G0 B0 ?)
我想知道如何从字符串和 RGB 颜色等来源计算十六进制值和字节 (0x--)(比如我如何找出 R255 G0 B0 的 0x 代码?)
Why do we use things like FF, is it to compensate for the base 10 system to get a number like 10?
为什么我们使用FF之类的东西,是不是为了补偿基数为10的系统来得到像10这样的数字?
采纳答案by Jonathon Reinhart
Hexadecimal is base 16, so instead of counting from 0 to 9, we count from 0 to F. And we generally prefix hex constants with 0x. Thus,
十六进制是以 16 为基数的,因此我们不是从 0 到 9 计数,而是从 0 到 F 计数。我们通常在十六进制常量前加上0x. 因此,
Hex Dec
-------------
0x00 = 0
0x09 = 9
0x0A = 10
0x0F = 15
0x10 = 16
0x200 = 512
A byte is the typical unit of storage for values on a computer, and on most all modern systems, a byte contains 8 bits. Note that bitactually means binary digit, so from this, we gather that a byte has a maximum value of 11111111 binary. That is 0xFF hex, or 255 decimal. Thus, one byte can be represented by a minimum of two hexadecimal characters. A typical 4-byte intis then 8 hex characters, like 0xDEADBEEF.
字节是计算机上存储值的典型单位,在大多数现代系统中,一个字节包含 8 位。请注意,bit实际上意味着binary digit,因此,我们从中得出一个字节的最大值为 11111111 二进制。那是 0xFF 十六进制,或 255 十进制。因此,一个字节至少可以用两个十六进制字符来表示。典型的 4 字节int是 8 个十六进制字符,例如0xDEADBEEF.
RGB values are typically packed with 3 byte values, in that order, RGB. Thus,
RGB 值通常包含 3 个字节的值,按此顺序为 RGB。因此,
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:
对于我的十六进制计算,我喜欢使用 python 作为我的计算器:
>>> 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 bit-shiftingto easily calculate those values:
对于 RGB 示例,我可以演示如何使用位移来轻松计算这些值:
>>> R=66
>>> G=0
>>> B=248
>>>
>>> hex( R<<16 | G<<8 | B )
'0x4200f8'
>>>
回答by dasblinkenlight
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.
Base-16(也称为hex)表示法很方便,因为您可以在一个十六进制数字中容纳四位,这使得转换为二进制非常容易,但不需要像完整二进制表示法那样多的空间。当您需要以人类可读的形式表示面向位的数据时,这很有用。
Learning hex is easy - all you need to do is memorizing a short table of 16 rows defining hex-to-binary conversion:
学习十六进制很容易——你需要做的就是记住一个由 16 行组成的短表,定义了十六进制到二进制的转换:
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
回答by Lee
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.
在十进制中,每个数字的权重是右边一位的 10 倍,例如 32 中的 '3' 是 3 * 10,而 102 中的 '1' 是 1 * 100。二进制类似,只是因为只有两位数(0 和 1),每一位的权重仅为右边一位的两倍。十六进制使用 16 位数字 - 10 位十进制数字以及字母 A = 10 到 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.
n 位十进制数可以表示最多 10^n - 1 的值,类似地,n 位二进制数可以表示最多 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.
十六进制很方便,因为您可以用 4 位表示单个十六进制数字,因为 2^4 = 16 个可能的值可以用 4 位表示。
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
您可以通过一次从正确的 4 位开始分组并将每个组转换为相应的十六进制来将二进制转换为十六进制。例如 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
从十六进制到二进制的转换更加简单,因为您可以简单地将每个十六进制数字扩展为相应的二进制,例如 0xA4 -> 1010 0100
回答by Cornelius
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.
发布的实际问题的答案(“为什么我们使用 FF 之类的东西,是不是为了补偿以 10 为基数的系统来获得像 10 这样的数字?”)是这样的:计算机使用位,这意味着 1 或 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.
本质类似于Lee发布的并称为“位置符号”。在十进制数中,数中的每一位代表10的幂。例如,在数123中,最后一位代表10^0——个位。中间位置代表 10^1——十位。第一个是 10^2——数百个。所以数字“123”代表 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.
二进制数字使用相同的系统。数字 10(以 2 为底)代表 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.
如果你想用二进制表示十进制数 10,你会得到数字 1010。这意味着,你需要四位来表示一个十进制数字。
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.
但是使用四位,您最多可以表示 16 个不同的值,而不仅仅是 10 个不同的值。如果您需要每个数字 4 位,您不妨使用以 16 为基数的数字,而不仅仅是以 10 为基数。这就是十六进制发挥作用的地方。
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).
关于如何转换ARGB值;正如其他回复中所写,二进制和十六进制之间的转换相对容易(4 个二进制数字 = 1 个十六进制数字)。
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.
十进制和十六进制之间的转换更复杂,至少对我来说,首先将十进制转换为二进制表示,然后将二进制数转换为十六进制更容易(如果我必须在我的脑海中这样做)。谷歌可能有大量的操作方法和算法。

