What is the difference between 0x and '\x' in C++? [duplicate] - c++

This question already has answers here:
What does \x mean in C/C++?
(7 answers)
strlen - the length of the string is sometimes increased by 1
(1 answer)
Closed 8 years ago.
I know that hex-decimal number is usually prefixed with 0x in C/C++ language.
For example, 0x5A means 90 in decimal.
But I saw an example code using single-quoted character with '\x'.
BYTE outputBuffer[index++] = '\x5A'; // instead of 0x5A
Is the meaning of '\x5A' exactly the same as 0x5A?
If so, why is there alternative way of hex-decimal notation?

For a character, both are quite equal.
But only one can be mixed into a string with other normal characters. "ABC\x5A"
And only one can be used to initialize a large integral type: long long x = 0x1234567812345678LL;

Related

C++ string length [duplicate]

This question already has answers here:
What does the C++ standard state the size of int, long type to be?
(24 answers)
Getting the actual length of a UTF-8 encoded std::string?
(11 answers)
Closed 2 years ago.
I was reading the documentation of string::length and, as you can see, it says that
Returns the length of the string, in terms of bytes.
So my question is, is this ensured to be also the number of char that it contains?
I know that usually a char is 1 byte, but is this ensured somewhere? Like in the standard or somewhere else?

Find sequence of chars in string c++ and erase it [duplicate]

This question already has answers here:
Replace part of a string with another string
(17 answers)
Closed 6 years ago.
I need find all occurrences of sequence: \r\n(some hex number)\r\n and delete this sequences from my string. Hexadecimal number doesn't start with 0x or x. It's just 20bb for example.
These sequences are chunks in http 1.1 protocol. I can't find them with string.find, maybe some regex would help.
Thanks for help.
From the code here I made this:
std::string string("\r\n20BB\r\n");
string = std::regex_replace(string,
std::regex("\r\n[0-9A-Fa-f]+\r\n"), "");
It should work. The [0-9A-Fa-f]+ captures one or more hex digits.

In C++, how do I take a string formatted like "######" and store each character as an integer? [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 7 years ago.
My function takes a large number as input, in the form of a string. I need to store each number in an integer array, but have been unable to do so. When I do a for loop and make array[i] = string[i], it saves the number as its ascii value. I've been trying to convert this number from its ascii value to an integer, but I cant get atoi to work. Any suggestions?
Since the numerals '0'-'9' are required to be encoded consecutivly in base character set, the numeric value of a numeral character c is simply c - '0'.

Meaning of L near text in define [duplicate]

This question already has answers here:
What exactly is the L prefix in C++?
(3 answers)
Why is letter L used to indicate wide chars strings?
(2 answers)
Closed 8 years ago.
I came across something I can't search for what it means.
#define DEFAULT_KEY L"text"
What does 'L' mean or do?
Other example that I found
wcscat(xpath, L"\\");
It means the string literal has type const wchar_t*, which is a type different that const char*. It is usually used to store Unicode strings.
Some APIs (particularly the Windows API) use this type all over the place.
The L prefix denotes a wide character/string literal; i.e., it is of type wchar_t instead of char. Unicode based programs typically use wide strings, while ANSI/ASCII based programs typically do not.

How does C++ understand two letter characters [duplicate]

This question already has answers here:
Multicharacter literal in C and C++
(6 answers)
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 9 years ago.
C++ seems to allow up to four characters to be held in single quotes, such as:
char c = 'abcd';
but at runtime, only the last value ('d') seems to be actually stored away. This behavior seems to happen for pairs of two, three, or four (at five the compiler finally calls uncle). But what's the deal with this design? I don't really see the logic in it.