Strange extra f added to bytes object
Terry Reedy
tjreedy at udel.edu
Sun Oct 6 20:46:20 EDT 2013
On 10/6/2013 6:47 PM, Robert Jackson wrote:
> I am very new to python so I'll apologize up front if this is some
Welcome to a mostly great language.
> boneheaded thing. 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 b'\x03\x66\x02\x01\xaa\xbb' it evaluates
> to b'\x03f\x02\x01\xaa\xbb', which doesn't even seem valid.
Python (or at least cpython) uses ascii chars to display bytes when
possible. This is often helpful, but not always ;-0.
>>> b'\x66' == b'f'
True
When you have b'\x03\x66\x02\x01\xaa\xbb', which is the same as
b'\x03f\x02\x01\xaa\xbb', the latter is used for display. So you do not
really have a problem. Some various thing you can do to get various
printouts.
>>> b=b'\x03f\x02\x01\xaa\xbb'
>>> list(b)
[3, 102, 2, 1, 170, 187]
>>> ' '.join('%x' % (c,) for c in b)
'3 66 2 1 aa bb'
>>> ''.join('\\x%x' % (c,) for c in b)
'\\x3\\x66\\x2\\x1\\xaa\\xbb'
21 chars; each \\ represents one \ char,
--
Terry Jan Reedy
More information about the Python-list
mailing list