Powershell ToBase64String& Linux base64 [英] Different output between Powershell ToBase64String & Linux base64
问题描述
SGVsbG8sIHdvcmxkIQ ==我必须编写两个脚本,一个用于Windows Server,另一个用于Ubuntu Server。为了说明,如果我的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编码有关,但我找不到一个解决方案。我可以解码Linux中的PowerShell输出,但是我不能在Linux中编码字符串,并获得与PowerShell相同的结果。一个bash解决方案是最佳的,但是一个C / C ++代码解决方案或指南也是如此。
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.
推荐答案
$ c> iconv :
You need to use 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屋!