InvalidCharacterError

3

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

But I need to do 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 encode this string with Base64 €, is there any other way to do this?

    
asked by anonymous 20.12.2017 / 18:08

3 answers

4

See this Documentation , has the 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"
    
20.12.2017 / 18:28
4

The problem is with this '€' character. According to w3schools this method only accepts the characters "AZ", "az", "0-9", "+", "/" and "="

Reference: link

    
20.12.2017 / 18:11
4

Try this below. Click the blue Run button below 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:<spanid="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>
    
20.12.2017 / 18:45