我的base64串联字符串有=个字符.如何摆脱它们 [英] My base64 concatenated string has = characters. How to get rid of them

查看:139
本文介绍了我的base64串联字符串有=个字符.如何摆脱它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何连接base64字符串以摆脱"="字符?

How should I concatenate base64 strings in order to get rid of the "=" characters ?

我从servlet发送了字节流[]数据作为http响应,并且在客户端我想打开pdf查看器.但是,由于这些额外的字符,我无法查看它.

I sent a byte stream[] of data from the servlet as a http response, and at the client side I want to open the pdf viewer. But, I can't view it because of these extra characters.

我试图用+ =,join和concat进行串联,但是每个子字符串的末尾仍然有=字符.

I tried to concatenate with +=, with join, with concat, but I still have the = character at the end of each substring.

也许有一种方法可以在没有最后一个字符的情况下连接字符串.

Maybe if there's a way to concat the strings without the last character.

推荐答案

标准的base-64编码将三个字节(3 * 8位)编码为4个字符(4 * 6位).如果原始数据中的字节数不能被3整除,则在余数为1的情况下添加2个=字符,在余数为2的情况下添加1 =个字符.

The standard base-64 encoding encodes three bytes (3 * 8 bits) into 4 characters (4 * 6 bits). If the number of bytes in the original data is not divisible by 3, 2 = characters are added if the remainder was 1, and 1 = is added if the remainder was 2.

现在,不幸的是,如果第一个以填充字符=结尾的字符串不能连接2个base-64编码的字符串-您必须同时解码两个字符串,并连接二进制字符串*,然后重新编码,否则后一部分将不可用同步,第二部分的所有字节将被错误地解码.

Now, unfortunately you cannot concatenate 2 base-64 encoded strings if the first ends with padding characters = - you must decode both, concatenate the binary string*, and then re-encode, otherwise the latter part will be out of sync and all bytes of the second part will be decoded incorrectly.

[*]不一定非要重新编码第一部分,但对其进行优化似乎仍然不值得.

[*] it is not strictly necessary to reencode the first part but optimizing for that still does not seem worthwhile.

这篇关于我的base64串联字符串有=个字符.如何摆脱它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆