0

I want to encode an integer into a short string using Base64 and return the value to Open Refine (Google Refine).

I found examples but they always give me an error.

import base64
foo = base64.b64encode('1')
return foo

works returning "MQ=="

But I want to encode the integer 1. The folowing code gives me an error.

import base64
foo = base64.b64encode(bytes([1]))
return foo

The example I found is here: How to encode integer in to base64 string in python 3

1 Answer 1

1

You can use binary data in string in form of \xx where xx is hexadecimal representation of byte.

With Python2 (including Jython2) try:

foo = base64.b64encode('\01')

for Python3:

foo = base64.b64encode(b'\01')

My result: AQ==

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

1 Comment

Thanks @Michał. I wasn't familiar with Python byte types.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.