Convert array of byte values to base64 encoded string and break long lines, Javascript (code golf)

前端 未结 3 982
不思量自难忘°
不思量自难忘° 2020-12-11 04:56

This JavaScript function takes an array of numbers (in the range 0-255) and converts to a base64-encoded string, then breaks long lines if necessary:

functio         


        
3条回答
  •  鱼传尺愫
    2020-12-11 05:30

    I have another entry:

    function encode(data)
    {
        var str = String.fromCharCode.apply(null,data);
        return btoa(str).replace(/.{76}(?=.)/g,'$&\n');
    }
    

    Minified, 88 characters:

    function e(d){return btoa(String.fromCharCode.apply(d,d)).replace(/.{76}(?=.)/g,'$&\n')}
    

    Or if you want trailing newlines, 85 characters:

    function e(d){return btoa(String.fromCharCode.apply(d,d)).replace(/.{1,76}/g,'$&\n')}
    

提交回复
热议问题