Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different output between Powershell ToBase64String & Linux base64

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

the result is QkFTRTY0. If my PowerShell Script runs:

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

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.

like image 338
1e100 Avatar asked Mar 31 '14 17:03

1e100


1 Answers

You need to use iconv to convert from UTF-8 to UTF-16 without a byte order mark:

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

The UTF16LE causes it to omit the BOM.

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

like image 120
Roger Lipscombe Avatar answered Sep 21 '22 16:09

Roger Lipscombe