Invalidcharactererror

Asked

Viewed 119 times

3

I have a variable inputParams with the string "Teste StackOverflow€"

But I need to do the btoa() of this variable and the error is occurring:

Failed to execute 'btoa' on 'Window': The string to be encoded contains characters Outside of the Latin1 range.

I need to make the encounter of this string with the € for Base64, there is some other way to do this?

3 answers

4

  • 1

    I need this character '€'.

  • 1

    Here is a demo for your troubleshooting: https://jsfiddle.net/m7v2ejzm/

  • 1

    worked thanks @Netinho Santos

4


Look at this Documentation, has Encode and Decode for Base64 with Unicode characters :

function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
        function toSolidBytes(match, p1) {
            return String.fromCharCode('0x' + p1);
    }));
}

b64EncodeUnicode('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
b64EncodeUnicode('\n'); // "Cg=="

To Decode Back to String use:

function b64DecodeUnicode(str) {
    return decodeURIComponent(atob(str).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

b64DecodeUnicode('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
b64DecodeUnicode('Cg=='); // "\n"
  • Copy the examples here, it will make it easier for people.

4

Try this below. Click the blue button Execute down there to test.

function unicodeEscape(str) {
    var map = {
        '\n': '\\n',
        '\\': '\\\\',
        '\f': '\\f',
        '\t': '\\t',
    };
    return str.replace(/[\s\S]/g, function(ch) {
        if (map[ch]) return map[ch];
        var code = ch.charCodeAt();
        if (code >= 32 && code <= 126) return ch;
        var u = ch.charCodeAt().toString(16),
            t = u.length > 2;
        return '\\' + (t ? 'u' : 'x') + ('0000' + u).slice(t ? -4 : -2);
    });
}

function unicodeUnescape(str) {
    var map = {
        '\\n': '\n',
        '\\r': '\r',
        '\\\\': '\\',
        '\\f': '\f',
        '\\t': '\t',
    };
    var hexMap = "0123456789abcdef";
    return str.replace(/\\x[0-9a-f][0-9a-f]/g, function(ch) {
        return String.fromCharCode(
                hexMap.indexOf(ch.charAt(2)) * 16 +
                hexMap.indexOf(ch.charAt(3))
        );
    }).replace(/\\u[0-9a-f][0-9a-f][0-9a-f][0-9a-f]/g, function(ch) {
        return String.fromCharCode(
                hexMap.indexOf(ch.charAt(2)) * 4096 +
                hexMap.indexOf(ch.charAt(3)) * 256 +
                hexMap.indexOf(ch.charAt(4)) * 16 +
                hexMap.indexOf(ch.charAt(5))
        );
    }).replace(/\\n/g, "\n").replace(/\\f/g, "\f").replace(/\\n/g, "\f").replace(/\\t/g, "\t").replace(/\\r/g, "\r").replace(/\\\\/g, "\\\\");
}

var teste = "Teste StackOverflow€";
var escape = unicodeEscape(teste);
var codificado = btoa(escape);
var decodificado = atob(codificado);
var desescapado = unicodeUnescape(decodificado);

$("#original").html(teste);
$("#escape").html(escape);
$("#codificado").html(codificado);
$("#decodificado").html(decodificado);
$("#desescapado").html(desescapado);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Original: <span id="original"></span></p>
<p>Escape: <span id="escape"></span></p>
<p>Codificado: <span id="codificado"></span></p>
<p>Decodificado: <span id="decodificado"></span></p>
<p>Desescapado: <span id="desescapado"></span></p>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.