Why does bytes.fromhex() treat some hex values strangely?

2024/10/2 22:27:55

I'm trying to use the socket library in Python to send bytes of two hex digits to a piece of hardware programmed to accept them. To create the bytes from a user-entered string of hex digits, I'm trying to use bytes.fromhex() method described here.

Why does the following:

hexstring = "bb 0D 02 55 55 5A ce"
data = bytes.fromhex(hexstring)
print(data)

give me:

b'\xbb\r\x02UUZ\xce'

instead of:

b'\xbb\x0d\x02\x55\x55\x5a\xce'

?

And how do I get it to produce the second output? I'm using Python 3.5.

Answer

This has nothing to do with bytes.fromhex(). You'd get the same result if you entered your expected result into Python:

>>> b'\xbb\x0d\x02\x55\x55\x5a\xce'
b'\xbb\r\x02UUZ\xce'

The repr() representation of a bytes object will always use ASCII printable characters and short one-letter escape sequences where possible.

So \x0d is displayed as \r, because that's the ASCII code point for a carriage return. \x55 is the printable ASCII character U, etc.

If this is an issue for you, you'll have to explicitly convert your bytes value to hexadecimal again:

>>> b'\xbb\r\x02UUZ\xce'.hex()
'bb0d0255555ace'
https://en.xdnf.cn/q/70798.html

Related Q&A

Extracting beats out of MP3 music with Python

What kind of solutions are there to analyze beats out of MP3 music in Python? The purpose of this would be to use rhythm information to time the keyframes of generated animation, export animation as v…

Switch to popup in python using selenium

How do I switch to a popup window in the below selenium program. Ive looked up for all possible solutions but havent been able to get my head around them. Please help!!from selenium import webdriver fr…

Segmentation fault while redirecting sys.stdout to Tkinter.Text widget

Im in the process of building a GUI-based application with Python/Tkinter that builds on top of the existing Python bdb module. In this application, I want to silence all stdout/stderr from the consol…

Why do pandas and dask perform better when importing from CSV compared to HDF5?

I am working with a system that currently operates with large (>5GB) .csv files. To increase performance, I am testing (A) different methods to create dataframes from disk (pandas VS dask) as well a…

is there any pool for ThreadingMixIn and ForkingMixIn for SocketServer?

I was trying to make an http proxy using BaseHttpServer which is based on SocketServer which got 2 asynchronous Mixins (ThreadingMixIn and ForkingMixIn)the problem with those two that they work on each…

Python - Read data from netCDF file with time as seconds since beginning of measurement

I need to extract values from a netCDf file. I am pretty new to python and even newer this file format. I need to extract time series data at a specific location (lat, lon). I have found that there is …

PyQt Multiline Text Input Box

I am working with PyQt and am attempting to build a multiline text input box for users. However, when I run the code below, I get a box that only allows for a single line of text to be entered. How to …

Calculate the sum of model properties in Django

I have a model Order which has a property that calculates an order_total based on OrderItems linked by foreign key.I would like to calculate the sum of a number of Order instances order_total propertie…

Set Host-header when using Python and urllib2

Im using my own resolver and would like to use urllib2 to just connect to the IP (no resolving in urllib2) and I would like set the HTTP Host-header myself. But urllib2 is just ignoring my Host-header:…

Full-featured date and time library

Im wondering if anyone knows of a good date and time library that has correctly-implemented features like the following:Microsecond resolution Daylight savings Example: it knows that 2:30am did not exi…