Home > Software design >  meaning of different dat byte format in python
meaning of different dat byte format in python

Time:11-12

From the CAN DBC file im taking few Can Messages and assigning values to each signal of Can Messages and then encoding it to send the can message.

for each signal, data is encoded in the different(few examples are listed below)

Data1--> b'\x00\x00d\x01P\x00\x00\x01'
Data2--> b'\x01\x81\x0f\x11\xc8\x00\x00\x00'
Data3--> b'\x00\x00d\x01P\x00\x00\x01'

My doubt is.. what is the meaning of x00d,x01P,x00d these data, what dose it denotes (what is P and d).

please let me know why data byte is in such format (like: x00d,x01P,x00d) and how to get the data as normal/usual format(like:Data2--> b'\x01\x81\x0f\x11\xc8\x00\x00\x00' )

CodePudding user response:

The d and P values you see in your bytestrings are bytes that coincidentally happen to represent ASCII characters. So Python displays them that way. If you want to see the raw hexidecimal values for all of the bytes, you can call the bytes.hex() method on the bytestring, it will give you output like: '0000640150000001' (where the 64 replaces d and 50 replaces P).

But if you're just passing around the bytes object, you probably don't need to do anything. Code that expects a bytes object will find b'd' just as acceptable as b'\x64', since they are two ways of writing the same character.

CodePudding user response:

This is exactly like C string constants. When the byte is a printable ASCII value, it shows you the ASCII. When it isn't, it shows you the hex. So, your first and third strings represent the hex bytes 00 00 64 01 50 00 00 01. Your second is 01 81 0f 11 c8 00 00 00.

Mostly, you shouldn't worry about this This is simply how Python prints byte strings if you do not format it yourself.

  • Related