Octal 10-hexadecimal 16 in CPP

Source: Internet
Author: User
Tags arabic numbers

1. Neither c nor C ++ provides the expression of binary numbers.

2. How do I express an octal number in C and C ++??

If the number is 876, we can conclude that it is not an octal number, because the octal number cannot contain more than 7 Arabic numbers. However, if the number is 123, 567, or 12345670, it is possible to set the number to octal or decimal.

So, C, C ++ specifies that if a number is specified to use octal, a value of 0 must be added before it.For example, 123 is in decimal format, but 0123 indicates octal.

Int 0123;

This is the expression of Octal numbers in C and C ++. But one exception is the escape letter '\'.

Because C and C ++ do not allow the use of a slash plus a 10-digit number to represent characters, so:

'? '// The ASCII value is 63
'\ 077' // is an octal representation '? ', 0 can be omitted, because C, C ++ does not allow the use of a slash plus a 10-digit number to represent characters
'\ 0x3f' // hexadecimal representation '? '

3. C, C ++ requires that the hexadecimal number must start with 0x.

Int 0x15a

X is not case sensitive. (Note: 0 in 0x is the number 0, not the letter O ).

Note:

1) The octal and hexadecimal values can only be unsigned positive integers. If you are in the Code:-078, or write:-0xf2, C, c ++ does not regard it as a negative number.

2) In QT, convert the decimal integer value to a hexadecimal string.

   int a = 63;     QString s = QString::number(a, 16);                   // s == "3f"     QString t = QString::number(a, 16).toUpper();     // t == "3F"

3) qstring stores hexadecimal values

// Output the string in hexadecimal format: qstring cmd = 0x0a; qdebug () <"cmd:" <cmd. toascii (). tohex ();

4) convert qstring to hexadecimal according to string surface format

QString str = "FF";  bool ok;  int hex = str.toInt(&ok, 16);       // hex == 255, ok == true 0xFF  int dec = str.toInt(&ok, 10);       // dec == 0, ok == false  

4) qbytearray stores hexadecimal values

Static const char mydata [] = {0x00, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76, 0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99, 0x6d, 0x5b}; qbytearray BD = qbytearray: fromrawdata (mydata, sizeof (mydata); qdebug () <"BD. data: "<BD. data (); qdebug () <"BD. tohex (): "<BD. tohex (); // output hexadecimal value

5) qchar stores hexadecimal values and prints

Qchar c = 0x0a; qbytearray array; array. append (c); qdebug () <array. tohex (); // The result is "0a"

6) char * stores hexadecimal and prints

Char C [] = {0x0a, 0x0b, '\ 0'}; qbytearray array (c); qdebug () <array. tohex (); // result "0a0b"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.