painterguy said:
hi Grep,
i compiled your code. here is output:-
http://img140.imageshack.us/img140/5426/grepcode.jpg
what is escape character, hex number and hex value? i am sorry i am asking many questions here. hope you will not mind. many thanks for the help.
I don't mind questions in the least. It's a pleasure to have someone who wants to learn, and your questions are good ones. So don't worry, ask away.
I wonder if the Windows console now uses UTF-8 characters. I suspect that may be the case now. Does this work?
std::cout << "x\xC2\xB2\n";
If that works, then there's a table of values for it here:
http://www.utf8-chartable.de/
As for the other questions, I'll start with escape characters. Ok, say you're designing a way of specifying a string of characters (like a C++ compiler, input file, etc). So you decide you want to delimit it with double-quote characters (i.e. "). Perfect, now you can do this, and you know where the string starts and ends:
"This is a string."
Now you want the string to be:
This is a quote character: "
So you would encode the string like this:
"This is a quote character: ""
Do you see the problem? It will interpret the first " character as the end of the string. You won't get your quote and any program parsing this will get confused and probably think there's an extra " character that shouldn't be there. So you need a way to tell it that the first quote should be interpreted in a special way. Enter the escape character. It's often (as in C/C++) the backslash character. You would encode it like this:
"This is a quote character: \""
Of course, if you want the string to contain an actual backslash, you need to escape it as well with... another backslash! Like so:
"This is a backslash character: \\"
Which is why newline is \n. You can't type in the newline character in the string, so you use the escape character to tell it that you don't mean the character n, but rather \n which means a newline character. Actually, technically, \n is a linefeed character, which causes a newline in Unix systems. In Windows, the end of line is really \r\n, but the compiler is smart enough to output \r\n (carriage return + line feed) on Windows systems when it sees \n.
As for hex (short for hexadecimal), that has to do with number bases. Normal numbers we're used to are base-10. Binary numbers are base-2, and hex numbers are base-16. Instead of counting "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...", you now have 16 rather than 10 characters to represent a digit. So we add "A, B, C, D, E, F" as digits and we now count like so: "1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, ..."
Here's a wiki page on hexadecimal:
http://en.wikipedia.org/wiki/Hexadecimal
It's handy because one digit in hex corresponds to 4 bits in binary. So 8 bits requires 2 hex digits, and so on. It's easy with a bit of practice to convert binary to hex and vice-versa. For example, converting binary to hex:
0101 1010
0101 is 5 (in both decimal and hex) and 1011 is 10 in decimal or A in hex. So the hex number is just:
5A
It's just as easy to convert back to binary. It's a very useful skill to know, and isn't as hard to learn as one might think at first glance.
Hope that helps!