views:

2231

answers:

7

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;
}
+2  A: 

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

Michael E
+12  A: 

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

Tommy Carlier
+3  A: 

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

In this case, it is just some numbering.

Paxinum
A: 

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

Ed Schembor
+3  A: 

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." );
tvanfosson
+18  A: 

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

Abel
I sometimes wonder whether this type of answers is better than answers with links to the guides. Should stack overflow rewrite the whole internet?
abyx
Lol, I sometimes wonder that too (but the links to the guides are in). Sometimes, beginners don't know how to search for something (i.e., search for "0xA" instead of "hexadecimal numbers") and end up asking. Instead of pointing them to numerous faqs, I believe it is worthwhile to summarize the parts of interest, plus pointing them to further reading.
Abel
Well, kudos. I never would have had the patience to summarize a thing that has been documented so many times before +1 :)
abyx
+1  A: 

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.

Mike Dunlavey