windows 为什么 DWORD 值通常以十六进制表示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5655283/
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
Why are DWORD values commonly represented in Hexadecimal?
提问by GreeneCreations
I am trying to understand why a DWORD value is often described in Hexadecimal on MSDN.
我试图理解为什么在 MSDN 上经常以十六进制描述 DWORD 值。
The reason why I am analyzing this is because I am trying to understand fundamentally why all these different number data types exist. A local mentor alluded to me that the creation of DWORD and other Microsoft types had something to do with the evolution of processors. This gives meaning and context to my understanding of these data types. I would like more context and background.
我之所以分析这个,是因为我试图从根本上理解为什么所有这些不同的数字数据类型都存在。一位当地导师向我暗示,DWORD 和其他 Microsoft 类型的创建与处理器的发展有关。这为我对这些数据类型的理解提供了意义和背景。我想要更多的背景和背景。
Either way, I could use some explanation or some resources on how to remember the difference between DWORD, unsigned integers, bytes, bits, WORD, etc.
无论哪种方式,我都可以使用一些解释或一些资源来了解如何记住 DWORD、无符号整数、字节、位、WORD 等之间的区别。
In summary, my questions are: 1) Why are DWORDs represented in Hex? 2) Can you provide resources on the differences between numerical data types and why they were created?
总之,我的问题是:1)为什么 DWORD 以十六进制表示?2) 您能否提供有关数值数据类型之间差异及其创建原因的资源?
回答by Tim
Everything within a computer is a bunch of 0s and 1s. But writing an entire DWORD in binary is quite tedious:
计算机中的一切都是一堆 0 和 1。但是用二进制编写整个 DWORD 非常乏味:
00000000 11111111 00000000 11111111
to save space and improve readability, we like to write it in a shorter form. Decimal is what we're most familiar with, but doesn't map well to binary. Octal and Hexadecimal map quite conveniently, lining up exactly with the binary bits:
为了节省空间并提高可读性,我们喜欢以较短的形式编写它。十进制是我们最熟悉的,但不能很好地映射到二进制。八进制和十六进制映射非常方便,与二进制位完全对齐:
// each octal digit is exactly 3 binary digits
01 010 100 binary = 124 octal
// each hexadecimal digit is exactly 4 binary digits
0101 0100 binary = 54 hexadecimal
Since hex lines up very nicely with 8-bit Bytes (2 hex digits make a Byte), the notation stuck, and that's what gets used most. It's easier to read, easier to understand, easier to line up when messing around with bitmasks.
由于十六进制与 8 位字节(2 个十六进制数字构成一个字节)非常吻合,因此符号卡住了,这就是最常用的。使用位掩码时更容易阅读、更容易理解、更容易排列。
The normal shorthand for identifying which base is being used:
识别使用哪个碱基的普通简写:
1234543 = decimal
01234543 = octal (leading zero)
0x1234543 = hexadecimal (starts with 0x)
As for your question about BYTE, WORD, DWORD, etc...
至于你关于字节、字、双字等的问题......
Computers started with a bit. Only 1 or 0. He had a cameo in the original Tron.
计算机开始有点。只有 1 或 0。他在最初的 Tron 中有一个客串。
Bytes are 8 bits long (well, once upon a time there were 7-bit bytes, but we can ignore those). This allows you to have a number from 0-255, or a signed number from -128 to 127. Better than just 1/0, but still limited. You may have heard references to "8-bit gaming". This is what we refer to. The system was built around Bytes.
字节有 8 位长(好吧,从前有 7 位字节,但我们可以忽略这些)。这允许您拥有 0-255 之间的数字,或 -128 至 127 之间的有符号数字。比仅 1/0 好,但仍然有限。您可能听说过对“8 位游戏”的提及。这就是我们所指的。该系统是围绕字节构建的。
Then computers grew to have 16-bit registers. This is 2 Bytes, and became known as a WORD (no, I don't know why). Now, numbers could be 0-65535 or -32768 to 32767.
然后计算机发展到具有 16 位寄存器。这是 2 个字节,后来被称为 WORD(不,我不知道为什么)。现在,数字可以是 0-65535 或 -32768 到 32767。
We continued to want more power, and computers were expanded to 32-bit registers. 4 Bytes, 2 Words, also known as a DWORD (double-word). To this day, you can look in "C:\Windows" and see a directory for "system" (old 16-bit pieces) and "system32" (new 32-bit components).
我们继续想要更多的功能,计算机扩展到 32 位寄存器。4 Bytes, 2 Words,也称为DWORD(双字)。直到今天,您可以在“C:\Windows”中查看“system”(旧的 16 位组件)和“system32”(新的 32 位组件)的目录。
Then came the QWORD (quad-word). 4 WORDS, 8 Bytes, 64 bits. Ever hear of the Nintendo-64? That's where the name came from. Modern architecture is now here. The internals of the cpu contain 64-bit registers. You can generally run a 32- or 64-bit operating system on such cpus.
然后是 QWORD(四字)。4 个字,8 个字节,64 位。听说过 Nintendo-64 吗?这就是名字的由来。现代建筑就在这里。cpu 的内部包含 64 位寄存器。您通常可以在此类 CPU 上运行 32 位或 64 位操作系统。
That covers Bit, Byte, Word, Dword. Those are raw types, and are used often for flags, bitmasks, etc. If you want to hold an actual number, it's best to use signed/unsigned integer, long, etc.
这涵盖了位、字节、字、双字。这些是原始类型,通常用于标志、位掩码等。如果你想保存一个实际数字,最好使用有符号/无符号整数、长整数等。
I didn't cover floating point numbers, but hopefully this helps with the general idea.
我没有涵盖浮点数,但希望这有助于总体思路。
回答by David Heffernan
DWORD constants are typically written in hex when they are used as flags that can be OR'd together in bitwise fashion. It makes it easier to see that is so. That's why you see 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 etc. Programmers just recognise those values as having binary representations with just a single bit set.
当 DWORD 常量用作可以按位方式进行 OR 运算的标志时,它们通常以十六进制编写。它更容易看出是这样。这就是为什么您会看到 0x01、0x02、0x04、0x08、0x10、0x20 等。程序员只是将这些值识别为只有一个位集的二进制表示。
When it's an enumeration you'd see 0x01, 0x02, 0x03 etc. They'd often still be written in hex because programmers tend to get into these habits!
当它是枚举时,您会看到 0x01、0x02、0x03 等。它们通常仍以十六进制编写,因为程序员往往会养成这些习惯!
回答by ecyrbe
Just for the record, 16 bit unsigned data are named WORD beacause at the time being, computers had 16 bits registers.
只是为了记录,16 位无符号数据被命名为 WORD,因为当时计算机有 16 位寄存器。
In computer history, 8 bits data where the biggest data you could store on a register. As it could store an ascii character it was commonly called a CHAR.
在计算机历史上,8 位数据是您可以存储在寄存器中的最大数据。因为它可以存储一个 ascii 字符,所以它通常被称为 CHAR。
But 16 bits computer came out and CHAR was not appropriate to name 16 bits data. So 16 bits data was commonly called a WORD because it was the biggest unit of data you could store on one register and it was a good analogy to continue the one made for CHAR.
但是 16 位计算机出现了,CHAR 不适合命名 16 位数据。所以 16 位数据通常被称为 WORD,因为它是您可以存储在一个寄存器中的最大数据单元,并且继续为 CHAR 制作的数据是一个很好的类比。
So, On some computers, using a different CPU WORD commonly refers to the size of the register. On Saturn CPU, wich use 64 bit register, a WORD is 64 bits.
因此,在某些计算机上,使用不同的 CPU WORD 通常是指寄存器的大小。在 Saturn CPU 上,使用 64 位寄存器,一个 WORD 是 64 位。
When 32 bits x86 processors came out, WORD stayed 16 bit for compatibility reasons, and the DWORD was created to extend it to 32 bits. The same is true for QWORD and 64 bits.
当 32 位 x86 处理器问世时,WORD 出于兼容性原因保持 16 位,并且创建了 DWORD 以将其扩展到 32 位。QWORD 和 64 位也是如此。
As for why hexadecimal is commonly used to describe a WORD, it has to do to the nature of the definition of a WORD that is tied to it's register origin. In assembler programming you use hexadecimal to describe data, because processors only know binray intergers, (0 and 1). And hexadecimal is a more compact way to use binary and still keeping some of it's properties.
至于为什么通常使用十六进制来描述 WORD,这与与寄存器来源相关的 WORD 定义的性质有关。在汇编程序中,您使用十六进制来描述数据,因为处理器只知道二进制整数(0 和 1)。十六进制是一种更紧凑的方式来使用二进制并仍然保留它的一些属性。
回答by ZarathustrA
You have very interesing and tricky question.
你有一个非常有趣和棘手的问题。
In short there was two drivers that lead to existing of to competetive type families - DWORD-based and int-based:
简而言之,有两个驱动因素导致了竞争类型家族的存在——基于 DWORD 和基于 int:
1) Desire to have crosspltformity on the one hand and the stricktly size types on the another hand.
1)一方面希望拥有交叉形式,另一方面希望拥有严格的尺寸类型。
2) Peoples conservatism.
2)民族保守主义。
In any case to provide ful detailed answer to you question and good enough background of this field we must dig into the computers history. And start our story from the early days of computing.
无论如何,要为您的问题提供完整详细的答案以及该领域足够好的背景,我们必须深入研究计算机的历史。从计算的早期开始我们的故事。
For the first, there is such a notion as a machine word. Machine word is a stricktly sized chunk of binary data that is natural for processing in the particular processor. So, size of the machine word is hardly processor dependent and in general equal to size of the geneal internal processor registers. Usually it can be subdivided into the two equal parts that also can be accessed by processor as independent chuncks of data. For example, on the x86 processors the machine word size is 32 bits. Thats mean that all general registers (eax, ebx, ecx, edx, esi, edi, ebp, esp and eip) have the same size - 32 bits. But many of them can be accessed as part of the register. For example you can access eax as 32 bit data chunk, ax, as 16 bit data chunk or even al as 8 bit data chunk. But not that physically this is all one 32 bit register. I think that you can found very good background on that field on Wikipedia (http://en.wikipedia.org/wiki/Word_(computer_architecture)). In short, machine word is how much bit data chunk can be used as an integer operand for the single instruction. Even today different processor architectures have different machine word size.
首先,有一个机器字这样的概念。机器字是一个严格大小的二进制数据块,在特定处理器中自然处理。因此,机器字的大小几乎不依赖于处理器,通常等于通用内部处理器寄存器的大小。通常它可以细分为两个相等的部分,也可以作为独立的数据块被处理器访问。例如,在 x86 处理器上,机器字大小是 32 位。这意味着所有通用寄存器(eax、ebx、ecx、edx、esi、edi、ebp、esp 和 eip)都具有相同的大小 - 32 位。但其中许多可以作为寄存器的一部分进行访问。例如,您可以将 eax 作为 32 位数据块访问,将 ax 作为 16 位数据块访问,甚至将 al 作为 8 位数据块访问。但并不是在物理上这都是一个 32 位寄存器。我认为您可以在维基百科 (http://en.wikipedia.org/wiki/Word_(computer_architecture)) 上找到有关该领域的非常好的背景资料。简而言之,机器字就是有多少位数据块可以用作单条指令的整数操作数。即使在今天,不同的处理器架构也有不同的机器字长。
Ok, we have some understanding of the computer word. It is a time to come back into the history of computing. The first Intel x86 processors that was popular had 16 bit word size. It came to the market at 1978. On that time the assembler was highly popular if not a primary programming language. As you know assembler is just a very thin wrapper under the native processor language. Due to this it is enterely hardware dependent. And when Intel push they new 8086 processor into the market, the first thing that they was needed to achive success is to push the assempler for the new processor into the market too. Nobody wants a processor that is nobody know how to program. And when Intel gave the names for the different data types in the assembler for the 8086 they make the evident chois and name 16-bit data chunk as a word, because the machine word of the 8086 have 16-bit size. The half of the machine word was called byte (8-bit) and two words used as one operand was called double word (32-bit). Intel used this terms in the processors manuals and in the assembler mnemonics (db, dw nd dd for statical allocation of byte, word and double word).
好了,我们对计算机这个词有了一些了解。现在是回到计算历史的时候了。第一个流行的 Intel x86 处理器具有 16 位字长。它于 1978 年上市。当时,汇编程序即使不是主要的编程语言,也非常流行。如您所知,汇编程序只是本机处理器语言下的一个非常薄的包装器。因此,它完全依赖于硬件。当英特尔将他们的新 8086 处理器推向市场时,他们要取得成功首先要做的就是将新处理器的组装商也推向市场。没有人想要一个没有人知道如何编程的处理器。当英特尔在 8086 的汇编器中给出不同数据类型的名称时,他们制作了明显的 chois 并将 16 位数据块命名为一个词,因为 8086 的机器字有 16 位大小。机器字的一半称为字节(8 位),用作一个操作数的两个字称为双字(32 位)。英特尔在处理器手册和汇编程序助记符中使用了这个术语(db、dw nd dd 用于字节、字和双字的静态分配)。
Years passed and 1985 Intel moved from 16-bit architecture to the 32-bit architecture with introduction of the 80386 processor. But at that time there was huge number of developers that was accustomed to that word is a 16-bit value. Beside that there was huge amount of soft was written with true belive that word is 16-bit. And many of the already written code rely to the fact that word is 16 bit. Due to this, beside the fact that the machine word size actually was changed, notation stayed the same, except the fact that new data type arrived to the assembler - quad word (64-bit), because the instruction that relies to the two machine words stayed the same, but the machine word was extended. In the same way the double quad word (128-bit) arrived now with 64-bit AMD64 architecture. As result we have
几年过去了,1985 年,随着 80386 处理器的推出,英特尔从 16 位架构转向了 32 位架构。但当时有大量的开发人员习惯了这个词是一个 16 位的值。除此之外,还有大量的软文是真的相信这个词是 16 位的。并且许多已经编写的代码依赖于 word 是 16 位的事实。由于这个原因,除了机器字大小实际上改变了,符号保持不变,除了新的数据类型到达汇编器 - 四字(64位),因为依赖于两个机器的指令词保持不变,但机器词被扩展了。以同样的方式,双四字(128 位)现在以 64 位 AMD64 架构出现。结果我们有
byte = 8 bit
word = 16 bit
dword = 32 bit
qword = 64 bit
dqword = 128 bit
Note the main thing in that type family is that it is strongly sized types family. Because it is come from and it heavily used in assembler, that require data types with constant size. Note, years pass ony by one but the data types from this family continue have the same constant size, beside the fact the its name already haven't its original meaning.
请注意,该类型系列中的主要内容是它是大尺寸类型系列。因为它来自并在汇编程序中大量使用,所以需要具有恒定大小的数据类型。请注意,一年过去了,但该系列的数据类型仍然具有相同的恒定大小,而且其名称已经没有其原始含义。
On the another hand, in the same time year by year, the high level languages became more and more popular. And because that languges was developed with cross-platform application in the mind thay looked to the sizes of its internal data types from absolutly other point of view. If I correctly understand no one high level language doesn't clearly claim that the some of its internal data types have a fixed constant size that never will be changed in the future. Let't look at the C++ as at the example. The C++ standart tells that:
另一方面,在逐年同一时间,高级语言变得越来越流行。而且因为语言是用跨平台应用程序开发的,所以从绝对其他的角度来看其内部数据类型的大小。如果我正确理解,没有一种高级语言不会明确声明其某些内部数据类型具有固定的常量大小,将来永远不会改变。让我们不要像在示例中那样看 C++。C++ 标准指出:
"The fundamental storage unit in the C++ memory model is the byte. A byte is at
least large enough to contain any member of the basic execution character set and
is composed of a contiguous sequence of bits, the number of which is implementa-
tion-defined. The least significant bit is called the low-order bit; the most
significant bit is called the high-order bit. The memory available to a C++ program
consists of one or more sequences of contiguous bytes. Every byte has a unique
address."
So, we can see surprising information - in C++ even byte haven't any constant size. So even if we accustomed to think have size - 8 bit, according to C++ can be not only 8 but also 9, 10, 11, 12 etc. bits in size. And maybe even 7 bit.
所以,我们可以看到令人惊讶的信息——在 C++ 中,甚至字节也没有任何恒定的大小。所以即使我们习惯于认为有大小 - 8 位,根据 C++ 不仅可以是 8 位,还可以是 9、10、11、12 位等。甚至可能是 7 位。
"There are five signed integer types: “signed char”, “short int”, “int”, and
“long int”., and “long long int”. In this list, each type provides at least as
much storage as those preceding it in the list. Plain ints have the natural size
suggested by the architecture of the execution environment; the other signed
integer types are provided to meet special needs."
That cite describe two main claims:
该引用描述了两个主要主张:
1) sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
1) sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
2) Plain ints have the natural size suggested by the architecture of the execution environment. That is mean that int must have the machine word size of the target processor architecture.
2) 普通整数具有执行环境架构建议的自然大小。这意味着 int 必须具有目标处理器架构的机器字大小。
You can goes through all C++ standard text but you will fail to find something like "size of int is 4 bytes" or "length of long is 64 bit". Size of particular integer C++ types can change with moving from one processor architecture to another, and with moving from one compiler to another. But even when you write program in c++ you will periodically faced with requirenment to use data types with well known constant size.
您可以浏览所有 C++ 标准文本,但您将无法找到诸如“int 的大小为 4 字节”或“long 的长度为 64 位”之类的内容。特定整数 C++ 类型的大小会随着从一种处理器架构转移到另一种处理器架构以及从一种编译器转移到另一种编译器而发生变化。但即使您用 C++ 编写程序,您也会定期面临使用具有众所周知的常量大小的数据类型的要求。
At least earlier compiler developers followed that standard claims. But now we can see that people conservatism comes into the play one more time. People used to think that int is 32-bit and can store values from the range from –2,147,483,648 to 2,147,483,647. Earlier when industry came through the border between 16-bit and 32-bit architectures. The second claim was strictly enforced. And when you used C++ compiler to create 16-bit program, compiler used int with 16-bit size that is "natural size" for 16-bit processors and, in contrast, when you used another C++ compiler to create 32-bit program but from the same source code, compiler used int with 32-bit size that is "natural size" for 32-bit processors. Nowadays, if you will look, for example, to the Microsoft C++ compiler you will found that it will use 32-bit int regardless of the target processor architecture (32-bit or 64-bit) just because people used to think that int is 32-bit!
至少早期的编译器开发人员遵循了该标准声明。但现在我们可以看到,人们的保守主义又一次出现了。人们过去认为 int 是 32 位的,可以存储从 –2,147,483,648 到 2,147,483,647 范围内的值。早些时候,当工业跨越 16 位和 32 位架构之间的边界时。第二项索赔得到了严格执行。当您使用 C++ 编译器创建 16 位程序时,编译器使用 16 位大小的 int,这是 16 位处理器的“自然大小”,相反,当您使用另一个 C++ 编译器创建 32 位程序但来自相同的源代码,编译器使用 32 位大小的 int,这是 32 位处理器的“自然大小”。现在,如果你看,例如,
As summury, we can see that thare are two data types families - dword-based and int-based. Motivation for the second one is obvious - cross-platform application development. Motivation for the fisrt one is all cases when the taking into the accaunt the sizes of variables have sense. For example, among other we can mention next cases:
作为总结,我们可以看到有两种数据类型系列 - 基于 dword 和基于 int。第二个的动机很明显——跨平台应用程序开发。第一个的动机是所有情况下,当考虑变量的大小时是有意义的。例如,除其他外,我们可以提到下一个案例:
1) You need to have some value in predifined well-known range and you need use it class or in another data structure that will populate into huge number of instances in run-time. In that case if you will use int-based types to store that value it will have the drawback in huge memory overhead on some architectures and potentially can broke the logic on another one. For example you need to manipulate values in the range from 0 to 1000000. If you will use int to store it, you program will correctly behave if int will be 32-bit, will have 4-byte memory overhead per each value instance if int will be 64-bit, and won't correctly work if int will be 16-bit.
1)您需要在预定义的众所周知的范围内有一些值,并且您需要在类或其他数据结构中使用它,这些数据结构将在运行时填充到大量实例中。在这种情况下,如果您将使用基于 int 的类型来存储该值,那么它会在某些架构上存在巨大内存开销的缺点,并且可能会破坏另一个架构上的逻辑。例如,您需要操作 0 到 1000000 范围内的值。如果您将使用 int 来存储它,那么如果 int 是 32 位,您的程序将正确运行,如果 int,则每个值实例将有 4 字节的内存开销将是 64 位,如果 int 将是 16 位,将无法正常工作。
2) Data involved into the nextworking. To have the ability to correctly handle your networking protocol on different PCs you will need to specify it in plain in size-based format, that will describe all packets and header bit by bit. Your network communication will be completely broken if on one PC you protocol header will be 20 byte length with 32-bit, and on another PC it will be 28 byte length with 64-bit int.
2) 涉及到下一步工作的数据。为了能够在不同的 PC 上正确处理您的网络协议,您需要以基于大小的格式以纯文本形式指定它,这将一点一点地描述所有数据包和标头。如果在一台 PC 上,您的协议标头长度为 20 字节长度为 32 位,而在另一台 PC 上长度为 28 字节长度为 64 位整数,那么您的网络通信将完全中断。
3) Your program need to store value used for some special processor instructions, or your program will communicate with modules or code chunks written in assembler.
3) 你的程序需要存储一些特殊处理器指令所使用的值,否则你的程序将与用汇编程序编写的模块或代码块进行通信。
4) You need store values that will be used to communicate with devices. Each device have its own specificetion that describes what sort of input device require as input and in what form it will provide output. If device require 16-bit value as input it must receive equally 16-bit value regardless of the int size and even regardless of machine word size used by processor on the system where device is installed.
4) 您需要存储将用于与设备通信的值。每个设备都有自己的规范,描述了什么样的输入设备需要作为输入以及它将以什么形式提供输出。如果设备需要 16 位值作为输入,则它必须接收同样的 16 位值,而不管 int 大小,甚至不管安装设备的系统上的处理器使用的机器字大小如何。
5) Your algoritm relies to the integer overflow logic. For example you have array of 2^16 entries, and you want infenitely and sequentely goes through it and refresh entries values. If you will use 16-bit int your program will works perfectly, but wimmediatelly you moving to the 32-bit int usage you will have out of range array index access.
5)您的算法依赖于整数溢出逻辑。例如,您有 2^16 个条目的数组,并且您希望无限地依次遍历它并刷新条目值。如果您将使用 16 位 int,您的程序将完美运行,但您很快就会使用 32 位 int,您将拥有超出范围的数组索引访问权限。
Due to this Microsoft use both families of data types. Int-based types in case where actual data size haven't big importance, and DWORD-based in the cases it have. And even in that case Microsoft define both as macroses, to provide the ability quickly and easy enough adopt virtual type system used by Microsoft to the particular processor architecture and/or compiler by assigning to it correct C++ equivalent.
因此,Microsoft 使用这两种数据类型系列。在实际数据大小不太重要的情况下基于 int 的类型,在它有的情况下基于 DWORD 的类型。即使在这种情况下,Microsoft 将两者都定义为宏,以通过为特定处理器架构和/或编译器分配正确的 C++ 等效项来提供足够快速和容易地将 Microsoft 使用的虚拟类型系统采用到特定处理器体系结构和/或编译器的能力。
I hope that I have covered the question about the origin of the data types and their differences quite well.
我希望我已经很好地介绍了有关数据类型的起源及其差异的问题。
So, we can switch to the seqond question about why hexademical digit is used to denote DWORD-based data types values. There are actually few reasons:
因此,我们可以切换到关于为什么使用十六进制数字来表示基于 DWORD 的数据类型值的第二个问题。其实有以下几个原因:
1) If we use stricktly-sized binary data types it will expectable enough that we can want to look on them in the binary form.
1) 如果我们使用严格大小的二进制数据类型,那么我们可以期望以二进制形式查看它们。
2) It is much easy to understand bit masks values when they encoded in binary form. Agree that it is much easier to understang what bit is set and what bit is reset if value in the next form
2) 当位掩码值以二进制形式编码时,很容易理解它们。同意如果下一个形式的值,更容易理解设置了哪些位以及重置了哪些位
1100010001011001
then if it will be encoded in next form
那么如果它将以下一个形式编码
50265
3) Data encoded in the binary form and described one DWORD-based value have the constant length, when the same data encoded in teh decimal form will have variable length. Note that even when the small number is encoded in binary form, the full value description is provided
3) 以二进制形式编码的数据和描述的一个基于 DWORD 的值具有恒定长度,当以十进制形式编码的相同数据将具有可变长度。请注意,即使小数以二进制形式编码,也会提供完整的值描述
0x00000100
instead of
代替
0x100
This property of the binary encoding is very attractive in the case when the analysis of the huge amount of binary data is required. For example, hex editor or analysis of the plain memory used by your program in debugger when your breakpoint was hit. Agree that it is much more comfortableto look on the neat columns of values that to the heap of weakly aligned variable size values.
二进制编码的这一特性在需要分析大量二进制数据的情况下非常有吸引力。例如,当您的断点被击中时,十六进制编辑器或分析程序在调试器中使用的普通内存。同意查看整齐的值列更舒服,这些列是弱对齐的可变大小值的堆。
So, we decided that we want to use binary encoding. We have three choices: use plain binary encoding, use octal encoding and use hexadecimal encoding. Peple prefere to use hexademical encoding because it most shortest from the set of avaliable encodings. Just compare
因此,我们决定使用二进制编码。我们有三种选择:使用纯二进制编码、使用八进制编码和使用十六进制编码。人们更喜欢使用十六进制编码,因为它是可用编码集中最短的。比较一下
10010001101000101011001111000
and
和
0x1234568
Can you quickly found the numbers of bits that is set in the next value?
您能快速找到下一个值中设置的位数吗?
00000000100000000000000000000
and in next?
下一步?
0x00100000
In the second case you can quickly divide the number in four separated bytes
在第二种情况下,您可以快速将数字分成四个分隔的字节
0x00 0x10 0x00 0x00
3 2 1 0
in each of which the first digit denote 4 most significant bits and the second one denote another 4 least significant bits. After you will spent some time on working with hex values you will remember the plain bit analog of each hexadecimal digit and will replace one by another in the mind without any problems:
其中第一个数字表示 4 个最高有效位,第二个数字表示另外 4 个最低有效位。在你花一些时间处理十六进制值之后,你会记住每个十六进制数字的纯位模拟,并且会在脑海中一个一个地替换,没有任何问题:
0 - 0000 4 - 0100 8 - 1000 C - 1100
1 - 0001 5 - 0101 9 - 1001 D - 1101
2 - 0010 6 - 0110 A - 1010 E - 1110
3 - 0011 7 - 0111 B - 1011 F - 1111
So, we need only second or two to found that we have bit number 20 is set!
所以,我们只需要一两秒钟就可以发现我们已经设置了位号 20!
People use hex because it is most short, confortable to undestand and use form of binary data encoding.
人们使用十六进制是因为它最简短,易于理解和使用二进制数据编码形式。
回答by Ana Betts
To elaborate on Tim's answer, it's because converting Hex to binary and back is very easy - each Hex digit is 4 binary digits:
详细说明蒂姆的答案,这是因为将十六进制转换为二进制并返回非常容易 - 每个十六进制数字是 4 个二进制数字:
0x1 = 0001
0x2 = 0010
...
0xD = 1101
0xE = 1110
0xF = 1111
So, 0x2D
= 0010 1101
所以,0x2D
=0010 1101