I am getting weird character in emacs when I open certain code. It is like ^M. I think it's something related to character encoding, or maybe not. Can something help me out here. Thanks.
that's a carriage return (CR) char (because the file probably has windows line endings: CRLF).
you can get rid of those line endings in emacs
M-x replace-string RET C-q C-M RET RET
Edit: my previous information was not quite right
If you want to set windows encoding on save, C-x <return> f dos <return> will set the file to save in dos encoding, so Windows users can read it properly. Similarly you can do C-x <return> f unix <return> to cause the file to change to unix formatting on save.
C-x <return> r dos yes
will switch over to dos mode in the current buffer (fixing line endings, and preserving formatting of the file), and is probably what you want.
When you open a file, and see the encoding is not right (i.e. has ^M characters everywhere, or unicode trash for instance), I find it easiest to M-x revert-buffer-with-coding-system RET dos RET
(replace dos with utf-16 or whatever you need).
This way you can open up the file in its preferred coding system. If you need to change the coding system, you can then do set-buffer-file-coding-system
(C-x RET f
) to switch it out to unix.