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

Community
  • 1
  • 1
Dizzley
  • 487
  • 6
  • 16

1 Answers1

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==

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114