简体   繁体   English

Base64编码btoa

[英]Base64 encoding btoa

I am encoding some text on my frontend part using btoa function: 我使用btoa函数在我的前端部分编码一些文本:

const encodedText = btoa(searchText);

This seems to work totally fine and decoding goes like this on backend part: 这似乎工作得很好,解码在后端部分就像这样:

byte[] decodedBytes = Base64.getDecoder().decode(searchedText);
String decodedString = new String(decodedBytes, Charset.defaultCharset());

Which also works fine. 这也很好。 However, this seems to fail when using ü letter. 但是,使用ü字母时,这似乎失败了。 My program encodes it as A==, and as far as I know, it should be w7w= 我的程序将其编码为A ==,据我所知,它应该是w7w =

I am not sure what I did wrong. 我不确定我做错了什么。

You could use 你可以用

const encodedText = btoa(unescape(encodeURIComponent(searchText)));

instead to encode unicode characters first. 而是首先编码unicode字符。

See Unicode strings and The "Unicode Problem" for further reading. 有关进一步阅读,请参阅Unicode字符串“Unicode问题”

 console.log(btoa('ü')); console.log(btoa(unescape(encodeURIComponent('ü')))); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM