0

How to convert this text to correct HTML characters using Javascript:

'PingAsyncTask - Token v\ufffdlido'

Put in your console:

console.log('PingAsyncTask - Token v\ufffdlido');

I already try all common functions:

  1. https://gist.github.com/chrisveness/bcb00eb717e6382c5608
  2. http://monsur.hossa.in/2012/07/20/utf-8-in-javascript.html
  3. http://jsfromhell.com/geral/utf-8

Can anyone help me?

2
  • console.log(unescape( encodeURIComponent('PingAsyncTask - Token v\ufffdlido'))); Let me know if it helps Commented Jul 3, 2015 at 5:10
  • You can use unescape function w3schools.com/jsref/jsref_unescape.asp Commented Jul 8, 2017 at 11:35

1 Answer 1

5

If your document is already UTF-8 you don't need to do anything special. The string is already encoded correctly in JavaScript, so when you write it into the document it'll show up correctly. You can see it in this fiddle: https://jsfiddle.net/baar4ew8/

P.S. The character in your code (\ufffd) is U+FFFD, the Unicode replacement character. Most fonts render it as a black diamond with a question mark inside, or just an empty box. Here's how Stack Overflow renders it:

If you're seeing that in your output, your string is being rendered correctly.

If you think you should be seeing some other character, then your problem isn't in the HTML or JavaScript—it's with the source of your data, whatever that might be. When a program converts text from a non-Unicode encoding to a Unicode encoding like UTF-8, characters that don't exist in Unicode are replaced with U+FFFD (�)—hence "replacement character." If you're expecting some character that does exist in Unicode but you're getting U+FFFD then it might be the case that the program converting the text to UTF-8 doesn't know what encoding it was originally in and so converted it incorrectly. For example, if you stored text with encoding X in a database table with encoding Y without first converting it to encoding Y.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.