11

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.

5
  • 3
    What about if you do [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('BASE64'))? Commented Mar 31, 2014 at 17:19
  • @ScottChamberlain, I have to get the same result of Unicode.GetBytes in Linux. Commented Mar 31, 2014 at 17:21
  • You can't get it to be the same because Linux uses utf8 and you are forcing Unicode and therefor have two different byte arrays. Why do you have the requirement to force Unicode encoding, using UTF8.GetBytes returns the correct result. Commented Mar 31, 2014 at 17:23
  • @ScottChamberlain I'm forced to use Unicode.GetBytes, because I have to exchange the result of my Bash script with a propietary Windows software that seems use Unicode.GetBytes. I'm not sure, because I don't have access to the source code, but if I use the strings generated by PowerShell i can interact with it in Windows without problem. But I need to do the same on my Ubuntu Server. Commented Mar 31, 2014 at 17:32
  • 2
    OK, so your question is actually: "how do I convert a string to UTF-16 on Ubuntu, and how do I get the base64 of that?" Commented Mar 31, 2014 at 17:47

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

1 Comment

With OSX 'iconv' the supported encoding switch is UTF-16LE. FWIW!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.