Path: csiph.com!weretis.net!feeder4.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.59.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Base64 encode using using windows-1250 encoding Date: Sat, 09 Jun 2018 09:53:48 +0200 Organization: PointedEars Software (PES) Lines: 49 Message-ID: <3701683.oWXzg6A8BD@PointedEars.de> References: <3469849.XUi3uDslzQ@PointedEars.de> <16824345.heBUIXtyyW@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="59.236.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.236.59"; logging-data="13432"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Jupi wrote: >> Thomas 'PointedEars' Lahn expressed precisely : >>> Jupi wrote: >>>> I need to encode string using Central European Windows-1250 encoding. >>> Why? >> >> Because I need it. > > No, you just *think* you need it. > > > >> […] >> I just need to encode some string which >> contains Easteuropean characters to base64 string. > > var b64 = window.btoa("Perišić"); This throws | Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to | be encoded contains characters outside of the Latin1 range. | at :1:8 in Chromium "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36". So apparently there is no easy way. Since ECMAScript implementations only use a Unicode-compatible 16-bit character encoding internally, a possibility appears to be convert the original string to a representation of UTF-8 first, encode the individual UTF-8 code units as if each of them were an ISO-8859-1 code unit, and then encode the resulting character string with Base64. For that, we could use var b64 = window.btoa( encodeURIComponent("Perišić").replace( /%([0-9A-F]{2})|./g, (m, code) => (code) ? String.fromCharCode(parseInt(code, 16)) : m) ); Still, the question remains: Why does Base64 have to be used in the first place? -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.