top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: Strange extra f added to bytes object

+2 votes
406 views

I am using python and pyserial to talk to an embedded pic processor in a piece of scientific equipment. I sometimes find the when I construct the bytes object to write it adds an extra f to the first byte.

For example if I have bx03x66x02x01xaaxbb it evaluates to bx03fx02x01xaaxbb, which doesnt even seem valid. Can anyone shine some light this?

posted Oct 7, 2013 by Deepak Dasgupta

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
"f" is the same as x66; nothing has been changed.

2 Answers

+1 vote
 >>> b'x66' == b'f'
True

Python always prints a bytestring the same way. It doesn't 'remember' how it was originally created. Another example:

 >>> b'x68x65x6cx6cx6fx20x77x6fx72x6cx64'
b'hello world'
answer Oct 7, 2013 by anonymous
+1 vote

b'x66' == b'f' . The hex for "f" is 66. The f isn't inserted, it's the second byte of your string. When Python displays a string, is uses the ASCII character if it can, and a hex escape if it can't. When you use a hex value that is a valid ASCII character, it will display the character.

answer Oct 7, 2013 by Sumit Pokharna
Similar Questions
+2 votes

Is it possible to say to a BufferedReader stream "give me all the bytes you have available in the buffer, or do one OS call and give me everything you get back"? The problem is that the "number of bytes" argument to read1() isn't optional, so I can't do available_bytes = fd.read1().

I need this because I want to decode the returned bytes from UTF-8, and I *might* get a character split across the boundary of any arbitrary block size I choose. (I'm happy to ignore the possibility that the *source* did a flush part-way through a character).

I don't really want to have to do incremental encoding if I can avoid it - it looks hard...

0 votes

I need to print the Dictionary in a detailed way.

Current When am print the dictionary am getting like below.

<Details.Value: object at 0x0000027062245160>
+1 vote

Can someone please explain data hiding in python through a program ?

...