How can ASCII representation of binary data be smaller than the binary representation?

Viewed 52

I was reading a page from Python's documentation related to sockets and I came a across this mysterious(for me) paragraph:

In these days of 64-bit machines, the ASCII representation of binary data is frequently smaller than the binary representation. That’s because a surprising amount of the time, most integers have the value 0, or maybe 1. The string "0" would be two bytes, while a full 64-bit integer would be 8. Of course, this doesn’t fit well with fixed-length messages. Decisions, decisions.

As a former Biomedical Engineer who doesn't have computer science background, I couldn't really get it("sorry in advance if it's too obvious to you"). In fact My main questions are:

  1. Why did it say: "The string "0" would be two bytes"? Doesn't it use one byte? ASCII uses 7 bits of a byte.
char = "0"
print(len(char.encode("ascii")))  # 1
  1. Why did it say: the ASCII representation of binary data is frequently smaller than the binary representation? Suppose I want to send number 200. in binary: I can easily use 1 byte to represent it in binary but with ASCII I should use 3:
number = 200
print(len(number.to_bytes(1, "big")))   # 1
print(len(str(number).encode("ascii"))) # 3
  1. Why did it say: "a surprising amount of the time, most integers have the value 0, or maybe 1": Why is that? What kind of data is he/she dealing with that mostly contains zeros and ones and not 3 for example.

  2. "a full 64-bit integer would be 8", Is it because we should always send a word to the network and not just bytes? Isn't it wasting bandwidths? Isn't it possible with delimiters?

0 Answers
Related