Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret hexadecimal numbers like 0x0A?

Tags:

c++

syntax

hex

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;
}
like image 401
Hyjker Avatar asked Nov 14 '09 14:11

Hyjker


People also ask

What does 0x0A mean?

LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.

How do you interpret hexadecimal numbers?

The first nine numbers (0 to 9) are the same ones commonly used in the decimal system. The next six two-digit numbers (10 to 15) are represented by the letters A through F. This is how the hex system uses the numbers from 0 to 9 and the capital letters A to F to represent the equivalent decimal number.

How do you read hexadecimal addresses?

Memory addresses are displayed as two hex numbers. An example is C800:5. The part to the left of the colon (C800) is called the segment address, and the part to the right of the colon (5) is called the offset. The offset value can have as many as four hex digits.

What is 0x10?

So 0x10 is hexadecimal for 16.


1 Answers

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

like image 89
Abel Avatar answered Sep 26 '22 03:09

Abel