Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 08-02-2016, 01:23 PM   #1
LQ Newbie
Registered: May 2012
Posts: 23

Rep: Reputation: Disabled
base64 encoding in program vs piping to base64 command-line utility

What's going on when I pipe a string into base64 on the command line vs base64 encoding in perl or python?

user@box:~/$ perl -MMIME::Base64 -e 'print encode_base64("user\\0user\\0test")';                                                                                                 

user@box:~/$ python -c "import base64; print(base64.b64encode('\\00test'))"

user@box:~/$ python -c "print('\\00test').strip()" | base64                                                                                                      
Old 08-03-2016, 04:54 PM   #2
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Look at python -m pydoc print

A "'\n'" character is written at the end, unless the "print" statement
ends with a comma.
Old 08-04-2016, 01:01 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,645

Rep: Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482
I knew that rang a bell, just couldn't remember until Rinndalir posted.

I came across the same thing a while back in bash
echo aaa |base64

echo -n aaa |base64

Last edited by chrism01; 08-08-2016 at 01:06 AM. Reason: typo
Old 08-05-2016, 10:39 AM   #4
LQ Newbie
Registered: May 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
I see. I don't understand how to provide a coma at the end of the print statement. It can be done in Python2 with a future import making that line a bit awkard to type. Thanks though, this clears up the mystery. I believed .strip() would have taken care of that "\n".

$ python -c "from __future__ import print_function; print('\\00test', end='')" | base64
TIL: python -m pydoc foo and why so many base64 encoded strings end in ==


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
program: Base64 timinator Other *NIX 1 07-16-2013 12:27 AM
Can I use base64 as a hash function? fantasy1215 Programming 8 01-13-2013 01:04 AM
[SOLVED] A question about generating base64 strings... trist007 Linux - Newbie 2 03-16-2011 01:21 PM
BASE64 APIs? EddyHahn Programming 0 11-29-2004 09:59 PM
Recommended XML / Base64 encoding libraries? overbored Programming 2 10-14-2004 11:21 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration