python


Convert byte[] to base64 and ASCII in Python


I am new to python and I'm quite struggling to convert and array of byte to base64 string and/or ASCII.
Tried googling stuff but can't seem to find a solution.
I can do this easily in C#, but can't seem to do this in Python 2.x/3.x
Any help is greatly appreciated.
Thanks in advance.
The simplest approach would be: Array to json to base64:
import json
import base64
data = [0, 1, 0, 0, 83, 116, -10]
dataStr = json.dumps(data)
base64EncodedStr = base64.b64encode(dataStr)
print(base64EncodedStr)
print('decoded', base64.b64decode(base64EncodedStr))
Prints out:
>>> WzAsIDEsIDAsIDAsIDgzLCAxMTYsIC0xMF0=
>>> ('decoded', '[0, 1, 0, 0, 83, 116, -10]') # json.loads here !
... another option could be using bitarray module.
This honestly should be all that you need: https://docs.python.org/3.1/library/base64.html
In this example you can see where they convert bytes to base64 and decode it back to bytes again:
>>> import base64
>>> encoded = base64.b64encode(b'data to be encoded')
>>> encoded
>>> b'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
>>> b'data to be encoded'
You may need to first take your array and turn it into a string with join, like this:
>>> my_joined_string_of_bytes = "".join(["my", "cool", "strings", "of", "bytes"])
Let me know if you need anything else. Thanks!

Related Links

Upload multiple files with Tornado Web Server and Nginx
How to read a simple binary file
How to run mysql in Django 1.5.5 with Python 3.3.4 in Windows 7
How can I do maps involving string fields in rethinkdb?
Scrollbar not functioning properly Tkinter
Is there a way to quickly create many files many sequential files (ex1 . . . ex 50) in Python?
Pygame playing sound with pygame.mixer.music.load(file) gives NoneType error
How to update multiple values in Mongodb using pymongo?
Manually return an error result and status failure for a celery task?
Python backtrace from C++ embedded in Python
Find nth smallest element in numpy array [duplicate]
How to extract array from the first element of triples in 2d array of triples using numpy
Simulate user clicking in QSystemTrayIcon
Parsing GeoRSS feed with python scripting
User interface optimization when using pack
Selenium doesn't return after starting Chrome but returns after starting Firefox?

Categories

HOME
tensorflow
google-apps-script
cntk
pug
reverse-engineering
google-tag-manager
youtube-livestreaming-api
reportportal
rfc
branch.io
eval
contact-form-7
oclint
hashicorp-vault
data-synchronization
automata
squarespace
ida
titan
sql-tuning
opera-mini
scenebuilder
session-timeout
boolean-expression
multicore
aws-cognito
sql-update
sensu
autolayout
piwik
spring-test
delete-file
bcrypt
philips-hue
bus-error
positioning
ioc-container
apache-cloudstack
getline
rhomobile
fabric-digits
openpgp
nlb
active-model-serializers
delphi-10.1-berlin
g1gc
bids
defold
applepayjs
android-bitmap
mcrypt
heroku-postgres
iphone-developer-program
opencpu
vici
verbose
sql-server-administration
manifest.mf
mongoose-populate
mailcatcher
uiprogressview
multifile-uploader
clob
spring-repositories
faraday
scriptlet
financial
interactive-brokers
git-rebase
approval-tests
void
throughput
plone-3.x
evo
ipojo
castle-windsor-3
twitter-rest-api
uptodate
hg-git
jython-2.7
hana-xs
microblaze
fragment-tab-host
google-admin-audit-api
firebug-lite
argument-passing
factors
anjuta
roxygen
cdata
vertical-rhythm
backbone-relational
xslkey
prng
pys60
scrollto
gjs
throttling
clients
odbc-sql-server-driver
cots

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App