5

Using Pythonnet in a C# application:

Python returns a bytes ({<class 'bytes'>}) object which is the result of a pickle.dumps operation.

What is the best way of dealing with this object in C# in terms of persistance to blob storage and rehydrating a bytes object to pass back to Python at a later stage?

cillierscharl
  • 7,043
  • 3
  • 29
  • 47

1 Answers1

7

Assuming you have access to the Python side of the equation, the easiest way to deal with these sorts of problem is to serialize the object in some sort of mutually understood format.

In this case, one idea would be to serialize the bytes into base64 (unicode such as UTF-8 or -16 can run into encoding issues depending on the contents of the byte string). Then you could convert that base64 bytes into UTF-8 to communicate it back across programs.

This looks like (for example):

base64.b64encode(pickle.dumps("Some data goes here")).decode("utf-8")
Eric Le Fort
  • 2,571
  • 18
  • 24
  • While this way works well it is not most efficient. There must be a more direct way to copy the bytes directly into a C# byte[]. – henon May 18 '20 at 20:02
  • Sure, tons of optimizations are possible. You could always stream bytes through a socket or pass along a (memory address, data length) pair for example. However, I doubt this will be the bottleneck of most programs. I'd make sure it actually is a bottleneck before bothering with a more complicated approach. – Eric Le Fort May 20 '20 at 04:33