Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
byte string in python2
#2
you can iterate through the string, and run  each character through ord():
example:
k = 'kryptonite'
' '.join(format(ord(letter), 'b') for letter in k)
result:
Output:
'1101011 1110010 1111001 1110000 1110100 1101111 1101110 1101001 1110100 1100101'
or for hex:
k = 'kryptonite'
' '.join(format(ord(letter), 'x') for letter in k)
Output:
'6b 72 79 70 74 6f 6e 69 74 65'
Reply


Messages In This Thread
byte string in python2 - by Skaperen - Nov-22-2017, 04:24 AM
RE: byte string in python2 - by Larz60+ - Nov-22-2017, 04:41 AM
RE: byte string in python2 - by Skaperen - Nov-22-2017, 05:08 AM
RE: byte string in python2 - by snippsat - Nov-22-2017, 01:13 PM
RE: byte string in python2 - by Skaperen - Nov-23-2017, 03:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 11,618 Sep-18-2020, 10:10 PM
Last Post: tienttt
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 31,741 Feb-08-2020, 06:58 PM
Last Post: karkas
  First Byte of a string is missing while receiving data over TCP Socket shahrukh1987 3 4,270 Nov-20-2019, 10:34 AM
Last Post: shahrukh1987
  Byte string catenation inefficient in 3.7? RMJFlack 13 5,749 Aug-18-2019, 05:19 AM
Last Post: RMJFlack
  Trying to run a python2 script dagamer1991 3 2,560 Aug-12-2019, 12:33 PM
Last Post: buran
  python2.7 executables thus the system python2.7 was erroring utility.execute()? vivekm 1 1,766 May-20-2019, 11:24 AM
Last Post: vivekm
  HELP: String of Zero's and One's to binary byte schwasskin 1 3,883 May-19-2019, 07:31 AM
Last Post: heiner55
  Python2 is not supported Skaperen 2 2,176 Mar-01-2019, 07:50 PM
Last Post: stranac
  4 byte hex byte swap from binary file medievil 7 22,167 May-08-2018, 08:16 AM
Last Post: killerrex
  get the content of the byte as string ricardons 5 3,705 Mar-02-2018, 02:41 PM
Last Post: ricardons

Forum Jump:

User Panel Messages

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