Python string representation of binary data

2024/10/4 23:33:45

I'm trying to understand the way Python displays strings representing binary data.

Here's an example using os.urandom

In [1]: random_bytes = os.urandom(4)In [2]: random_bytes
Out[2]: '\xfd\xa9\xbe\x87'In [3]: random_bytes = os.urandom(4)In [4]: random_bytes
Out[4]: '\r\x9eq\xce'

In the first example of random_bytes, after each \x there seem to be values in hexadecimal form: fd a9 be 87.

In the second example, however, I don't understand why '\r\x9eq\xce' is displayed.

Why does Python show me these random bytes in this particular representation? How should I interpret '\r\x9eq\xce'?

Answer

It's only using the \xHH notation for characters that are (1) non-printable; and (2) don't have a shorter escape sequence.

To examine the hex codes, you could use the binascii module:

In [12]: binascii.hexlify('\r\x9eq\xce')
Out[12]: '0d9e71ce'

As you can see:

  • \r is the same as \x0d (it's the ASCII Carriage Return character, CR);
  • q is the same as \x71 (the latter is the hex ASCII code of the former).
https://en.xdnf.cn/q/70547.html

Related Q&A

Combining Spark Streaming + MLlib

Ive tried to use a Random Forest model in order to predict a stream of examples, but it appears that I cannot use that model to classify the examples. Here is the code used in pyspark:sc = SparkContext…

How to select dataframe rows according to multi-(other column)-condition on columnar groups?

Copy the following dataframe to your clipboard:textId score textInfo 0 name1 1.0 text_stuff 1 name1 2.0 different_text_stuff 2 name1 2.0 text_stuff …

Python Recursive Search of Dict with Nested Keys

I recently had to solve a problem in a real data system with a nested dict/list combination. I worked on this for quite a while and came up with a solution, but I am very unsatisfied. I had to resort t…

Scrapy: how to catch download error and try download it again

During my crawling, some pages failed due to unexpected redirection and no response returned. How can I catch this kind of error and re-schedule a request with original url, not with the redirected url…

Cryptacular is broken

this weekend our docker image broke because it cannot be build anymore. While looking into the stats, I saw this line:crypt_blowfish-1.2/crypt.h:17:23: fatal error: gnu-crypt.h: No such file or directo…

how to run test against the built image before pushing to containers registry?

From the gitlab documentation this is how to create a docker image using kaniko: build:stage: buildimage:name: gcr.io/kaniko-project/executor:debugentrypoint: [""]script:- mkdir -p /kaniko/.d…

Adding a colorbar to a pcolormesh with polar projection

I am trying to add a colorbar to a pcolormesh plot with polar projection. The code works fine if I dont specify a polar projection. With polar projection specified, a tiny plot results, and the colorba…

GridSearch for Multi-label classification in Scikit-learn

I am trying to do GridSearch for best hyper-parameters in every individual one of ten folds cross validation, it worked fine with my previous multi-class classification work, but not the case this time…

Visualize tree in bash, like the output of unix tree

Given input:apple: banana eggplant banana: cantaloupe durian eggplant: fig:I would like to concatenate it into the format:├─ apple │ ├─ banana │ │ ├─ cantaloupe │ │ └─ durian │ └…

pygame.error: Failed loading libmpg123.dll: Attempt to access invalid address

music = pygame.mixer.music.load(not.mp3) pygame.mixer.music.play(loops=-1)when executing the code I got this error: Traceback (most recent call last):File "C:\Users\Admin\AppData\Local\Programs\Py…