js中如何将数组转化为base64encode的字符串?

数组就像这样:

[21,23,0,0,0,0,21,255,43,154,87,32,4,13,64,7,86,52,1,23,16,57,73,1]

若是btoa([21,23,0,0,0,0,21,255,43,154,87,32,4,13,64,7,86,52,1,23,16,57,73,1])
结果如下:
"MjEsMjMsMCwwLDAsMCwyMSwyNTUsNDMsMTU0LDg3LDMyLDQsMTMsNjQsNyw4Niw1MiwxLDIzLDE2LDU3LDczLDE="
atob("MjEsMjMsMCwwLDAsMCwyMSwyNTUsNDMsMTU0LDg3LDMyLDQsMTMsNjQsNyw4Niw1MiwxLDIzLDE2LDU3LDczLDE=")
"21,23,0,0,0,0,21,255,43,154,87,32,4,13,64,7,86,52,1,23,16,57,73,1"
转过去再转回来已经变成"21,23,0,0,0,0,21,255,43,154,87,32,4,13,64,7,86,52,1,23,16,57,73,1"这样的了。。

阅读 12k
2 个回答
var arry = [21, 23, 0, 0, 0, 0, 21, 255, 43, 154, 87, 32, 4, 13, 64, 7, 86, 52, 1, 23, 16, 57, 73, 1];
            var str = base64Encode(arry);//转换字符串
            console.log(str);
            var arry2= base64Decode(str).split(',');
            console.log(arry2);//转回数组

            function base64Encode(input) {
                var rv;
                rv = encodeURIComponent(input);
                rv = unescape(rv);
                rv = window.btoa(rv);
                return rv;
            }
            function base64Decode(input) {
                rv = window.atob(input);
                rv = escape(rv);
                rv = decodeURIComponent(rv);
                return rv;
            }

先 json stringify 最后再 parse

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题