I have Javascript in an XHTML web page that is passing UTF-8 encoded strings. It needs to continue to pass the UTF-8 version, as well as decode it. How is it possible to decode a UTF-8 string for display?
<script type="text/javascript">
// <![CDATA[
function updateUser(usernameSent){
var usernameReceived = usernameSent; // Current value: Größe
var usernameDecoded = usernameReceived; // Decode to: Größe
var html2id = '';
html2id += 'Encoded: ' + usernameReceived + '<br />Decoded: ' + usernameDecoded;
document.getElementById('userId').innerHTML = html2id;
}
// ]]>
</script>
I searched for a simple solution and this works well for me:
Only issue I have is sometimes I get one character at a time. This might be by design with my source of the arraybuffer. I'm using https://github.com/xseignard/cordovarduino to read serial data on an android device.
Here is a solution handling all Unicode code points include upper (4 byte) values and supported by all modern browsers (IE and others > 5.5). It uses decodeURIComponent(), but NOT the deprecated escape/unescape functions:
Tested and available on GitHub
To create UTF-8 from a string:
Tested and available on GitHub
Perhaps using the textDecoder will be sufficient.
Not supported in all browsers though. But it might be sufficient if you use crosswalk or any other use case where you know what browser is used.
Update @Albert's answer adding condition for emoji.
I reckon the easiest way would be to use a built-in js functions decodeURI() / encodeURI().
// String to Utf8 ByteBuffer
// Utf8 ByteArray to string