【问题标题】:Base64 decoding has "%" at the end of the result sometimes. Is it the result supposed to be? Any solution to that?Base64 解码有时在结果末尾有“%”。是应该的结果吗?有什么解决办法吗?
【发布时间】:2014-04-19 23:50:40
【问题描述】:

我只是在研究base64编码和解码算法,并尝试了一些程序。我在网上找到了一些示例代码,但结果对我来说有点奇怪。 这是链接:http://knol2share.blogspot.com/2011/07/base64-encoding-and-decoding-in-c.html

我尝试用它来编码和解码一个字符串。
输入一个字符串: 02613
Base64 编码值:MDI2MTM=
Base64解码值:02613% -- 不知道为什么会有“%”,有没有办法得到正确的结果

我什至在linux中尝试过Base64程序,去掉编码中的换行符后得到了相同的结果。

结果如下: %echo -n 02613 |base64
MDI2MTM=
%echo -n MDI2MTM= | base64 --解码
02613%

有谁知道我怎样才能得到与输入字符串完全相同的结果?谢谢。

【问题讨论】:

    标签: linux encoding base64


    【解决方案1】:

    如果解码后的文本不以换行符结尾,则会打印出来。

    $ printf "foobar\n" | base64 | base64 --decode
    foobar
    
    $ printf "foobar" | base64 | base64 --decode
    foobar%
    

    【讨论】:

      【解决方案2】:

      这是使用 echo 命令的示例,-n 选项将删除换行符,这就是为什么在第一种情况下我们没有符号 % 而在第二种情况下附加了一个。

      ➜  ~ echo "HELLO" | base64 | base64 --decode
      HELLO
      ➜  ~ echo -n "HELLO" | base64 | base64 --decode
      HELLO%
      

      【讨论】:

        【解决方案3】:

        % 符号不是命令提示符吗? 解码b64后添加新行并检查。

        【讨论】:

        • 对。我得到02613alfe@host:~$ 作为“输出”,因为我的提示是alfe@host:~$
        • 你可以在 Linux 上尝试echo "$(base64 --decode <<< MDI2MTM=)"
        猜你喜欢
        • 1970-01-01
        • 2015-10-10
        • 2012-01-21
        • 2016-01-28
        • 1970-01-01
        • 2013-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多