PowerShell的ToBase64String与放大器之间不同的输出; Linux的BASE64 [英] Different output between Powershell ToBase64String & Linux base64

查看:459
本文介绍了PowerShell的ToBase64String与放大器之间不同的输出; Linux的BASE64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SGVsbG8sIHdvcmxkIQ ==我必须写两个剧本,一个用于Windows Server和另一个Ubuntu服务器。举例来说,如果我的bash脚本运行:

SGVsbG8sIHdvcmxkIQ== I have to write two scripts, one for Windows Server and another for Ubuntu Server. To illustrate, if my bash script runs:

echo -n 'BASE64' | base64

结果是 QkFTRTY0 。如果我的PowerShell脚本运行:

the result is QkFTRTY0. If my PowerShell Script runs:

[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('BASE64'))

结果是 QgBBAFMARQA2ADQA 。重要的是获得在Linux中相同的PowerShell的字符串。任何人都知道为什么这种差异,有些解决我的问题?我认为这个问题是相关于Unix的UTF-8编码,但我不能找到一个解决方案。我可以去code Linux中的PowerShell的输出,但我不能带code在Linux中的字符串,并得到相同的结果的PowerShell。一个bash的解决方案是最优的,但C / C ++ code溶液或指导工作过。

the result is QgBBAFMARQA2ADQA. Is important get the same PowerShell string in Linux. Anybody knows why this difference and some solution to my problem? I think that the problem is related to Unix's UTF-8 encoding, but I can't find a solution. I can decode the PowerShell output in Linux, but I can't encode a string in Linux and get the same result as PowerShell. A bash solution is optimal, but a C/C++ code solution or guide works too.

推荐答案

您需要使用的iconv

$ echo -n 'BASE64' | iconv -f UTF8 -t UTF16LE | base64
QgBBAFMARQA2ADQA

UTF16LE 使其忽略BOM。

请参阅 http://superuser.com/q/381056/4206 ,如果​​有人问起强行包括BOM ,但你要相反,后者 - 方便 - 在这个问题

See http://superuser.com/q/381056/4206, where someone asks about forcibly including the BOM, but you want the opposite, which -- conveniently -- is in the question.

这篇关于PowerShell的ToBase64String与放大器之间不同的输出; Linux的BASE64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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