Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings

后端 未结 10 2122
野趣味
野趣味 2020-11-22 16:24

I\'m using the Javascript window.atob() function to decode a base64-encoded string (specifically the base64-encoded content from the GitHub API). Problem is I\'

10条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 16:40

    Small correction, unescape and escape are deprecated, so:

    function utf8_to_b64( str ) {
        return window.btoa(decodeURIComponent(encodeURIComponent(str)));
    }
    
    function b64_to_utf8( str ) {
         return decodeURIComponent(encodeURIComponent(window.atob(str)));
    }
    
    
    function b64_to_utf8( str ) {
        str = str.replace(/\s/g, '');    
        return decodeURIComponent(encodeURIComponent(window.atob(str)));
    }
    

提交回复
热议问题