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

发布于 2024-08-11 18:17:44 字数 346 浏览 1 评论 0 原文

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;
}

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

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

天荒地未老 2024-08-18 18:17:44

正如已经说过的,0x0A 等于十进制 10(十)。这等于 LF(换行)的 ASCII 代码,在许多系统上它是换行符。但就您而言,您使用 DisconnectClient,它采用 ULONG。无论您传递 0xA 还是仅传递 10 都不重要。

在本例中,该数字的含义客户端连接的接口

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

理解十六进制

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

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

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

在编程语言中,通常以 x0x< /code> 或 &h (取决于语言)表示十六进制数。与十进制数一样,前导零可以被忽略。尾随零有其明显的含义。

将十六进制转换为十进制

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

  1. 取每个十六进制数字,写下其十进制版本
  2. 将每个数字与 16^pos 相乘,其中 pos == 十六进制数字中的位置,最右边的位置为零
  3. 将结果相加

数字 0x8B20< /code> 变成:

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

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

关于数字及其基数还有很多要说的,但如果您需要更多信息,我建议您查看 数学论坛常见问题解答,或 维基百科(更一般)。要在多个碱基之间进行转换,请尝试此在线碱基-X 计算器

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

As has been said already, 0x0A is 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 0xA or just 10 doesn't matter.

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

EDIT: looking again at your code, your disconnectClient function is different from the one on MSDN. If it is a user defined method, finding out the meaning of 0x0A requires 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).

Understanding hexadecimal

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

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.

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.

In programming languages, it is common to start a number with x, 0x or &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.

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.

  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

The number 0x8B20 becomes:

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).

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.

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

坚持沉默 2024-08-18 18:17:44

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

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

や三分注定 2024-08-18 18:17:44

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

 #define REQUEST_FAILURE 0x02
 #define LOGIN_FAILURE 0x08

然后

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

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).

 #define REQUEST_FAILURE 0x02
 #define LOGIN_FAILURE 0x08

Then

 disconnectClient( REQUEST_FAILURE | LOGIN_FAILURE,
                   "You must enter your account number." );
北音执念 2024-08-18 18:17:44

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

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

蓝海似她心 2024-08-18 18:17:44

这是一个十六进制数,0x0A 与十进制数 10 相同。

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

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

In this case, it is just some numbering.

爱格式化 2024-08-18 18:17:44

这是十六进制数字 10。代表各种代码的数字通常以十六进制给出,以便您可以“看到这些位”。例如,相同的十六进制数字,但添加了 16 位,将是 0x1A。添加 256 位,则为 0x11A。以十进制表示,这将是 256+16+10=282,更难看出哪些位处于打开状态。

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.

贱人配狗天长地久 2024-08-18 18:17:44

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文