C++ 如何解释像 0x0A 这样的十六进制数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1734333/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:03:00  来源:igfitidea点击:

How to interpret hexadecimal numbers like 0x0A?

c++syntaxhex

提问by Hyjker

What does 0x0A mean in C++ and how should I interpret or read such hexadecimal values?

0x0A 在 C++ 中是什么意思,我应该如何解释或读取这样的十六进制值?

if (version < 760 || version > 760){
    disconnectClient(0x0A, STRING_CLIENT_VERSION);
}

uint32_t accnumber = msg.GetU32();
std::string password = msg.GetString();

if(!accnumber){
    disconnectClient(0x0A, "You must enter your account number.");
    return false;
}

回答by Abel

As has been said already, 0x0Ais equal to decimal 10(ten). This is equal to the ASCII code for LF (Line Feed), which is a newline on many systems. But in your case, you use DisconnectClient, which takes a ULONG. Whether you would pass 0xAor just 10doesn't matter.

正如已经说过的,0x0A等于十进制10(十)。这等同于 LF(换行)的 ASCII 代码,在许多系统上它是换行符。但在你的情况下,你使用DisconnectClient,它需要一个ULONG. 你是否通过0xA或只是10不重要。

The meaningof this number, in this case, is the interface on which the client is connecting.

在这种情况下,这个数字的含义客户端连接的接口

EDIT: looking again at your code, your disconnectClientfunction is different from the one on MSDN. If it is a user defined method, finding out the meaning of 0x0Arequires checking that method itself, or its documentation (though it is possible that it is just a stub to the "real" DisconnectClient, and simply passes on the parameter).

编辑:再次查看您的代码,您的disconnectClient功能与 MSDN 上的功能不同。如果它是用户定义的方法,则0x0A需要检查该方法本身或其文档(尽管它可能只是“真实”的存根DisconnectClient,并且只是传递参数)才能找出 的含义。

Understanding hexadecimal

理解十六进制

EDIT:In case you are wondering how all answerers here seem to know that hexadecimal 0x0Aequals decimal 10, read on:

编辑:如果您想知道这里的所有回答者似乎都知道十六进制0x0A等于十进制10,请继续阅读:

Hexadecimal numbers are base-16 (hexa = 6, deca = 10). We are nowadays accustomed to base-10, but history shows that base-20 (France still has quatre-vingt), base-5 (Russia) and others have been used even before the event of binary (base-2) numbers became common for computers. Base-16 is just as base-10, but now you don't have 10 fingers, but 16 instead.

十六进制数以 16 为基数(hex = 6,deca = 10)。我们现在已经习惯了基数为 10,但历史表明基数 20(法国仍然有 quatre-vingt)、基数 5(俄罗斯)和其他甚至在二进制(基数 2)数字变得普遍之前就已经使用了用于计算机。Base-16 和 base-10 一样,但现在你没有 10 个手指,而是 16 个。

Computers can only think in bits, four bits (a nibble) can make the numbers 0-15. To make it easier to read and write about bits, hexadecimal notation was used. This adds A-F to the ubiquitous digits 0-9, where A equals 10, B equals 11 until F equals 15.

计算机只能以位为单位思考,四位(半字节)可以使数字成为 0-15。为了更容易读取和写入位,使用了十六进制表示法。这将 AF 添加到无处不在的数字 0-9,其中 A 等于 10,B 等于 11,直到 F 等于 15。

In programming languages, it is common to start a number with x, 0xor &h(depending on the language) to denote a hexadecimal number. Leading zeroes, just as with decimal numbers, can be ignored. Trailing zeroes have their obvious meaning.

在编程语言中,通常以x,0x&h(取决于语言)开头的数字表示十六进制数。前导零,就像十进制数一样,可以忽略。尾随零有其明显的含义。

Transform hexadecimal into decimal

将十六进制转换为十进制

So, how would you go from a hexadecimal to a decimal number? Each digit should be multiplied to a power of 16, instead of a power of 10 for decimal. There's a simple generic formula to get from any base-X to any base-Y, but here's it applied to going from base-16 to base-10.

那么,如何从十六进制数转换为十进制数呢?每个数字都应该乘以 16 的幂,而不是十进制的 10 的幂。有一个简单的通用公式可以从任何基数 X 到任何基数 Y,但它适用于从基数 16 到基数 10。

  1. Take each hex digit, write its decimal version down
  2. Multiply each digit with 16^pos, where pos == position in hex number, right-most position is zero
  3. Add the results
  1. 取每个十六进制数字,写下它的十进制版本
  2. 将每个数字与 相乘16^pos,其中 pos == 十六进制数中的位置,最右边的位置为零
  3. 添加结果

The number 0x8B20becomes:

数字0x8B20变为:

8 * 16^3 = 8  * 4096 = 32768
B * 16^2 = 11 * 256  =  2816
2 * 16^1 = 2  * 16   =    32
0 * 16^0 = 0  * 1    =     0 
                     -------  +
                       35616

Rather tedious to do by hand, but you get the drift, I hope. If you have Windows, type Calc in the Run-Window or the search box (Vista, W7) and click View > Scientific. Now you can type hexadecimal numbers (hit F5) and switch between decimal (F6), octal (F7) and binary (F8).

手工制作相当乏味,但我希望你能理解。如果您使用的是 Windows,请在运行窗口或搜索框(Vista、W7)中键入 Calc,然后单击查看 > 科学。现在您可以输入十六进制数(按 F5)并在十进制 (F6)、八进制 (F7) 和二进制 (F8) 之间切换。

There's a lot more to say about numbers and their bases, but if you need more, I suggest you take a look at the math forum faq, or on Wikipedia (more general). To convert between many bases, try this online base-X calculator.

关于数字及其基数还有很多要说的,但如果您需要更多,我建议您查看数学论坛 faq维基百科(更一般)。要在多个基数之间进行转换,请尝试使用此在线 base-X 计算器

Update:added sections on understanding and transforming hexadecimal numbers, thought it was perhaps applicable ;-)

更新:添加了关于理解和转换十六进制数的部分,认为它可能适用;-)

回答by Tommy Carlier

0x is the prefix for hexadecimal notation. 0x0A is the hexadecimal number A, which is 10 in decimal notation.

0x 是十六进制表示法的前缀。0x0A 是十六进制数 A,十进制表示为 10。

回答by tvanfosson

I suspect that it is a status code being sent back to the client as part of the protocol that it is being used. Typically each bit that is set will represent some flag. In this case flags being set are the ones corresponding to the (low) 2 & 3 bits (counting from zero). A better way to handle this would be to define the flags as macros then simply using them (ORing to get combinations).

我怀疑它是作为正在使用的协议的一部分发送回客户端的状态代码。通常,设置的每个位都代表某个标志。在这种情况下,设置的标志是对应于(低)2 和 3 位(从零开始计数)的标志。处理此问题的更好方法是将标志定义为宏,然后简单地使用它们(ORing 以获得组合)。

 #define REQUEST_FAILURE 0x02
 #define LOGIN_FAILURE 0x08

Then

然后

 disconnectClient( REQUEST_FAILURE | LOGIN_FAILURE,
                   "You must enter your account number." );

回答by Michael Ekstrand

It's the hexidecimal number 0A. In decimal, it is 10; in binary, 00001010.

它是十六进制数 0A。十进制为10;二进制,00001010。

回答by Per Alexandersson

That is a number in hexadecimal, 0x0A is the same as the decimal number 10.

即十六进制数,0x0A与十进制数10相同。

In this case, it is just some numbering.

在这种情况下,它只是一些编号。

回答by Mike Dunlavey

It's the number 10 in hex. Often numbers that represent various codes are given in hex so that you can "see the bits". For example the same number in hex, but with the 16-bit added, would be 0x1A. Adding in the 256-bit, it would be 0x11A. In decimal this would be 256+16+10=282, much harder to see which bits are on.

它是十六进制的数字 10。通常代表各种代码的数字以十六进制给出,以便您可以“看到位”。例如,相同的十六进制数字,但添加了 16 位,将是 0x1A。加上 256 位,它将是 0x11A。在十进制中,这将是 256+16+10=282,很难看到哪些位在开启。

回答by Ed Schembor

That is a numeric literal in hexadecimal format. The 0x prefix denotes hex.

这是一个十六进制格式的数字文字。0x 前缀表示十六进制。