Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
\b
#1
i want to be able convert binary encoding into the appropriate character using a \b prefix just like \x for hexadecimal and \o for octal.  but \b is already used for backspace.  i am so depressed.  more beer!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
like this?

for p in "1111001 1101111 1110101 1110010 1100101 100000 1110111 1100101 1101100 1100011 1101111 1101101 1100101".split(): print(chr(int("0b"+p, base=2)))
the "youre welcome" isnt intended to be snarky, the code was actually for someone else i was talking to recently.
Reply
#3
i wrote a program that needed to convert human input to their binary rquivalent character code.  if the human types \ and r which results in a string of length 2 with \r in it. then my code converts that to a carriage return, the eqivalent of chr(13).  i made this handle all the sequences seen in man ascii as done in most Linux and most Unix.  these are codes from 7 to 13.  it also supports the sequences that begin with ^ making ^J be character code 10 (newline) and ^[ be character code 27 (escape).  i also added sequence \xNN where NN is interpreted as hexadecimal (documented in my code as "cetal") and the sequence \oMNN where MNN is interpreted as octal.  i wanted to add support for binary by doing \bNNNNNNNN but \b was already used (for character code 8, backspace).

the program takes the first argument as a screen (the screen program) session name and the remaining arguments as input to that screen session.  there is also a program to launch a background screen session (detached) with the specified name (required) and specified screen size (optional, default to the size this command is run in).

it also has support for \dMNN to interpret MNN as a decimal number for a character code as well as \MNN (digits 0, 1, 2, or 3 follow the \ character) as a traditional octal form.  letters may be given in either upper or lower case for all sequences.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
if youre stuck with \b for ascii 8, why not just use \0 for binary then?

\0 is often octal (fun to say several times) but you picked \o for that, so...

you could also try \2 but thats silly. still! yours to decide, eh?
Reply
#5
well i had decided that common sequences should work as expected and then i can expand on that.  that's where i thought that i wanted to add on a way to do binary bits.  i had even started coding it.  then i noticed the conflict around \b.  people are going to expect \b to be a backspace and a '0' or '1' following it to be handled as usual, not part of some escape sequence.  so, for now, my code does not have this (mis)feature.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
right now i am including these sequence conversions in a command script to send terminal input to a screen session.  later, i will make it available as a separate function.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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