#đź”’ Converting to 8bit ASCII

41 messages · Page 1 of 1 (latest)

short tendon
#

I’m asked to find 8bit ASCII of the string “attack at dawn”

steel viperBOT
#

@short tendon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

short tendon
#

Does int(“attack at dawn”) work?

#

I need to then convert it down to a binary

spare zenith
upper elk
#

ascii is kinda 7 bit though. what exactly do you want to achieve with that?

short tendon
#

yes, but I’m not sure if it’s 8bit or not

#

This is my task

#

I need to perform an XOR

#

i need to perform an xor between binary of 8 bit ascii of attack at dawn and the binary of the given hex

upper elk
#

then you need to encode it. and xor byte by byte.

short tendon
#

what does encode it mean?

upper elk
#

"attack at dawn" is a unicode string. encoding means converting it to bytes using some encoding or charset. e.g. you could use the ascii encoding for that

#

!e

text = "attack at dawn"
encoded = text.encode('ascii')
print(encoded)
print(text[0], '=', encoded[0])
print(text[1], '=', encoded[1])
steel viperBOT
short tendon
#

If I were to do it manually, I’ll need to check the ASCII of each character and write it each block of 8 bits right?

#

in binary

upper elk
#

if you want to write down the bits, then yes. ascii only covers the lower 7 bit of a byte, but you can just add an 8th bit to make it 8 bits.

turbid crag
#

(the 8th bit would be a 0)

short tendon
#

so each byte i can put an extra 0 ?

short tendon
upper elk
#

or more, depending on how many bits the ascii number occupies. e.g. a space has ascii 32, and only needs 6 bits.

short tendon
#

makes sense essentially add fillers to make it 8 bit to store each character

upper elk
#

(the thing with extra zeros at the higher places is really the same with any other number system.
The decimal number 185 is the same as 0185 or 0000000185)

short tendon
#

yep

#

How do I acheive this in python?

#

Can I do XOR in other bases?

#

Maybe i can convert ASCII to hex and then do xor in hex

upper elk
#

xor is a bitwise operator

short tendon
#

My final answer has to be in hex

short tendon
#

Not theoretically

turbid crag
#

decimal, binary, and hex are just different ways to express the same number, you perform operations on numbers not bases

upper elk
#

if you do it in python, you only have to make sure it's an integer type... the python xor operator does it on bitwise level... and the result will be an integer, that you can convert to a hex string using the hex() function.

short tendon
#

Yes, indeed

turbid crag
#

in the case of bitwise operators, it is easiest to do it by hand when you write out the number in binary, then you can convert that number to however you want to display it afterwards, i.e. hex

#

in a program, just deal with numbers without regard for base until you eventually need to display it

steel viperBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.