56

I need to send the value of some variables between two machines and intend to do it using sockets. I use the md5 hash algorithm as a checksum for the data I send to ensure the data is correctly transmitted. To perform the md5 hash algorithm I have to convert the data to bytes. I want to transmit both the name of the variable and its value. As I have a lot of variables i use a dictionary.

So I want to convert something like this to bytes?

variables = {'var1' : 0, 'var2' : 'some string', 'var1' : ['listitem1','listitem2',5]}

In other words I have a dictionary with a lot of different data types inside it including lists which in turn have multiple different data types in them and I want to convert that into bytes. Then on the receiving machine convert those bytes back into a dictionary.

I have tried a few different methods json is recomended here (Convert a python dict to a string and back) but I can't seam to produce a string with it never mind bytes.

Community
  • 1
  • 1
user1205406
  • 691
  • 1
  • 7
  • 15
  • 4
    How is your question different from the one you linked to? You say that you've tried "a few different methods". Focus on one. Tell us what you tried, show us your code, and tell us what didn't work for you. – Robᵩ Oct 07 '13 at 18:32
  • the one i liked to didn't provide an example – user1205406 Oct 07 '13 at 18:40
  • A bytestring is bytes. – Marcin Oct 07 '13 at 18:41
  • 3
    @user1205406: If you don't know how to read [the docs](http://docs.python.org/2/library/json.html) that someone already linked for you, then I don't think you're going to know how to read an example that someone copies from the top of those same docs, or an equivalent example that someone makes up from scratch. – abarnert Oct 07 '13 at 18:42
  • i'm aware of how to read the docs my question was actually is there a way to convert a dictionary directly to a bytestring without having to first convert it to a string – user1205406 Oct 07 '13 at 18:46
  • @user1205406: If that's true, why does your question say "I can't seam to produce a string with it never mind bytes"? Besides, every single method shown in the linked question produces bytes (either a `str` or a binary file/`StringIO`) in Python 2.x, and all of them but JSON also produce bytes (either a `bytes` or a binary file/`BytesIO`) even in 3.x. If you really read the docs and tried something and got stuck, then show us what you tried. – abarnert Oct 07 '13 at 18:52
  • I have nominated this question for reopening as the marked duplicate question only covers converting a dict to a string but not encoding that string into bytes which is the difference here. I've prepared an answer to this question using the convenience functions `str.encode(...)` and `bytes.decode(...)` (which are not mentioned in answers to either question currently), however, the question must be unlocked for me to have access to post it. The accepted answer today does not actually completely answer the question asked for this reason. – Taylor D. Edmiston Jun 20 '19 at 21:27
  • Your answer is https://stackoverflow.com/a/61358045/8584198. – Saeed Zahedian Abroodi Apr 22 '20 at 06:32

2 Answers2

59

This should work:

import json

s = json.dumps(variables)
variables2 = json.loads(s)
assert variables == variables2
Mike
  • 1,080
  • 1
  • 9
  • 25
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
26

If you need to convert the dictionary to binary, you need to convert it to a string (JSON) as described in the previous answer, then you can convert it to binary.

For example:

my_dict = {'key' : [1,2,3]}

import json
def dict_to_binary(the_dict):
    str = json.dumps(the_dict)
    binary = ' '.join(format(ord(letter), 'b') for letter in str)
    return binary


def binary_to_dict(the_binary):
    jsn = ''.join(chr(int(x, 2)) for x in the_binary.split())
    d = json.loads(jsn)  
    return d

bin = dict_to_binary(my_dict)
print bin

dct = binary_to_dict(bin)
print dct

will give the output

1111011 100010 1101011 100010 111010 100000 1011011 110001 101100 100000 110010 101100 100000 110011 1011101 1111101

{u'key': [1, 2, 3]}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
shshank
  • 2,571
  • 1
  • 18
  • 27