Python Forum
portion of the bytes to string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
portion of the bytes to string
#1
Hi, I'm not a programing guy and i have some difficulty doing something that i imagine is very simple for most of you

I have some bytes (encode as latin-1) like

b'f\x01\xff\xff\xff'
b'p308.95\xff\xff\xff'

In the first one I only want the x01 and get the numeric representation as integer
in the second I want to get the number in bolt as float

Also, Is any way to transform b'f\x01\xff\xff\xff' in the array [0x66, 0x01, 0xFF, 0xFF, 0XFF]

Any ideas

I don't know why don't i get something like
0x66 0x01 0xFF 0xFF 0XFF.
is x01 the same as 0x01?
Reply
#2
you can do some manipulations
>>> bx = b'f\x01\xff\xff\xff'
>>> bx.decode('latin-1')
'f\x01ÿÿÿ'
>>> list(bx)
[102, 1, 255, 255, 255]
>>> [hex(c) for c in bx]
['0x66', '0x1', '0xff', '0xff', '0xff']
>>> from binascii import hexlify
>>> hexlify(bx)
b'6601ffffff'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,651 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  How to delete portion of file already processed? Mark17 13 2,726 Jan-22-2022, 09:24 AM
Last Post: Pedroski55
  bytes f-string ? Skaperen 5 4,313 Jun-10-2021, 10:21 PM
Last Post: Gribouillis
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,190 May-02-2021, 03:45 PM
Last Post: Anldra12
  how to do a bytes f-string? Skaperen 4 11,054 Jul-16-2020, 11:25 PM
Last Post: Skaperen
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,597 May-08-2020, 04:26 AM
Last Post: bowlofred
  code not writing to projNameVal portion of code. umkc1 1 1,669 Feb-05-2020, 10:05 PM
Last Post: Larz60+
  printing a bytes string Skaperen 2 2,354 Jul-21-2019, 03:42 AM
Last Post: Skaperen
  replace bytes with other byte or bytes BigOldArt 1 10,607 Feb-02-2019, 11:00 PM
Last Post: snippsat
  Extracting a portion of a text document alarcon032002 8 4,311 Jan-17-2019, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020