Is Encoding the same as Escaping?
I am interested in theory on whether Encoding is the same as Escaping? According to Wikipedia
an escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence.
My current thought is that they are different. Escaping is when you place an escape charater in front of a metacharacter(s) to mark it/them as to behave differently than what they would have normally.
Encoding, on the other hand, is all about transforming data in开发者_高级运维to another form, and upon wanting to read the original content it is decoded back to its original form.
Escaping is a subset of encoding: You only encode certain characters by prefixing a special character instead of transferring (typically all or many) characters to another representation.
Escaping examples:
- In an SQL statement: ... WHERE name='O\' Reilly'
- In the shell:
ls Thirty\ Seconds\ *
- Many programming languages:
"\"Test\"
string (or"""Test"""
)
Encoding examples:
- Replacing
<
with<
when outputting user input in HTML - The character encoding, like UTF-8
- Using sequences that do not include the desired character, like
\u0061
fora
They're different, and I think you're getting the distinction correctly.
Encoding is when you transform between a logical representation of a text ("logical string", e.g. Unicode) into a well-defined sequence of binary digits ("physical string", e.g. ASCII, UTF-8, UTF-16). Escaping is a special character (typically the backslash: '\') which initiates a different interpretation of the character(s) following the escape character; escaping is necessary when you need to encode a larger number of symbols to a smaller number of distinct (and finite) bit sequences.
They are indeed different.
You pretty much got it right.
精彩评论