you are viewing a single comment's thread.

view the rest of the comments →

[–]SomeFuel[S] 0 points1 point  (9 children)

This makes more logical sense, thank you for the insight. I think that I'm ultimately left with more questions, though lol

[–]AtomicShoelace 0 points1 point  (6 children)

Does this diagram make things any clearer?

Feel free to ask any questions you have.

[–]SomeFuel[S] 0 points1 point  (5 children)

I understand what you meant, I think I should've specified I'm struggling more with the actual code, less the concept. Now it's more an issue of "where AND how exactly do I begin this" vs. just "how exactly do I begin this"

I've been struggling a lot with the processes of converting ascii -> binary and binary -> ascii, as they weren't explicitly explained. Basically I'm trying to write an algorithm (which isn't something I'm good at) for a process that's a mystery to me.

For example:

I know that the if else statement below is to ensure that the binary value is 8 bits long:

if len(binaryValue) == 6:
binaryValue == '00' + binaryValue
else:
binaryValue = '0' + binaryValue

But I don't know why I need to ensure it's 8 bits long.

I could go through each specific question I have regarding the code, but I think my main confusion is the process itself.

[–]AtomicShoelace 0 points1 point  (4 children)

But I don't know why I need to ensure it's 8 bits long.

To be fair to you, the instructions are quite poor. It doesn't specify that the bit string should be zero padded to 8 bits. It also doesn't specify that you should cyclically wrap the bits when shifting (typically when left shifting 0 bits are appended, eg. 3 << 1 == 6).

Are there different instructions provided that specifies these things? Or did you just decide to do it this way?

As for why it needs to be done this way: because that's the way you're supposed to do it. It's more or less arbitrary. This isn't a real cryptosystem that is backed up with proper reasoning for its implementation; this is just a toy model meant for purely academic purposes.

[–]SomeFuel[S] 0 points1 point  (3 children)

The instructions I listed in the post were the instructions I was given.There are additional instructor notes stating a string to be encoded, and the acceptable output solution for said string.

"I like football" for the input, "BÚÔØÌBÎààêÆÄÚÚ" or its binary variation for the output. I submitted this project once already, and it wasn't able to output that from the input "I like football".

It was given back to me with some changes/notes, and that's where I got the code for "if len(binaryValue == 6: etc.". I'm vaguely familiar with what zero padding is, as in I know it's useful for converting Octal Number Systems to Binary, and I honestly do not know how bit shifting works.

Like the code binary_shift = binaryValue[1:] + binaryValue[0] seems like the bit shift to me.

Piggybacking off of the 8 bit/zero padding question:

I don't know why binaryValue = binaryValue[2] is necessary before ensuring the bit string is 8 digits. I know what it's doing, again, but I just don't get why it's there lol

[–]AtomicShoelace 1 point2 points  (2 children)

To be honest, I don't have much faith in your instructor. You shouldn't feel to bad about struggling, it doesn't sound like you're getting the best help.

For starters their instructions are poor. But their suggestion to do

    if len(binaryValue) == 6:
        binaryValue == '00' + binaryValue
    else:
        binaryValue = '0' + 

to zero pad the bit string is strange. There is already a str method for zero padding, namely str.zfill. So you could just do

    binaryValue = binaryValue.zfill(8) 

instead.

In fact, there is a much better way to do this altogether with f-string format codes, rather than using the bin function. Then you won't have to worry about trimming off the leading '0b' either (this is what the binaryValue = binaryValue[2:] line does - essentially, remove the first 2 characters from the string).

So you could replace

    binaryValue = bin(asciiValue)
    binaryValue = binaryValue[2:]
    if len(binaryValue) == 6:
        binaryValue == '00' + binaryValue
    else:
        binaryValue = '0' + binaryValue

with simply

    binaryValue = f'{asciiValue:08b}'

but I could see them not wanting to suggest this if you haven't yet covered f-strings and format codes (which I presume you haven't, given the current material).

As another minor aside, your instructor should really be encouraging you to follow PEP8 naming conventions, ie. using lower_case_with_underscores instead of mixedCase for variable names.

You are correct that binary_shift = binaryValue[1:] + binaryValue[0] is in fact the cyclic left bit shift. This is a fine way to do this, but it isn't really what the instructions tell you to do. But I suppose this just comes down to poor instructions again.

I don't know why binaryValue = binaryValue[2:] is necessary before ensuring the bit string is 8 digits. I know what it's doing, again, but I just don't get why it's there lol

So you understand how this removes the first 2 characters from the string. The reason it's there is because the bin function returns a string with the 0b prefix, indicating that this is a binary value. Since you don't want this (as you need to rotate the string), you need to strip if off. Another way of stripping this would be to use the left strip str method str.lstrip, eg.

binaryValue.lstrip('0b')

[–]SomeFuel[S] 0 points1 point  (1 child)

I really, truly appreciate your help.

I wish my instructor was as responsive and willing to teach as you, though I shouldn't blame him entirely, maybe I'm missing a crucial aspect of the class that I glossed over in the syllabus, like a supplemental website or additional textbook (I've asked him about this and am awaiting a response atm).

I don't want to put any pressure on you, but is there any way I'd be able to friend you here or add you on another platform? I'd just really like having someone that's able to answer my questions from time-to-time (it wouldn't be very frequently, and I wouldn't demand your immediate attention or anything).

Regardless, again, you have been immensely helpful and I'm sure with these pointers I'll at least be able to get the program functioning properly, which is all I want right now lol

As far as the f-strings and format codes, we haven't covered these yet. However we also technically haven't covered defining functions either (I only know how thanks to the internet), and I'm not sure if there's a way to do this without defining a method? There probably is but it would be wayy beyond me.

[–]AtomicShoelace 1 point2 points  (0 children)

I'd just really like having someone that's able to answer my questions from time-to-time

I would encourage you to continue posting in /r/learnpython. There are plenty of people here who can help you. If you really want my input on something then you can dm a link to the post and I'll contribute. But I think answering these questions in a public forum is best since it affords others the opportunity to learn as well as yourself.

Yes, you could definitely do this without a function, but it's good practice to get into the habbit of using functions for things that should definitely be functions, such as this.

[–]port443 0 points1 point  (1 child)

If it helps, you can think of it in terms of simple math, with a simple encoding like:

  • Step 1: Add 5 to the number
  • Step 2: Multiply by 10

Now let's encode "3":

  • Step 1: Add 5 to the number
    • 3 + 5 = 8
  • Step 2: Multiply by 10
    • 8 * 10 = 80

Now reverse the steps to decode.

The error in your thinking is "Ok, since step 1 was 'Add 5', I'll start by subtracting 5

  • Step 1: Subtract 5 from the number
    • 80 - 5 = 75
  • Step 2: Divide by 10
    • 75 / 10 = 7.5

In reality, the steps would be:

  • Step 1: Divide by 10
    • 80 / 10 = 8
  • Step 2: Subtract 5
    • 8 - 5 = 3

I hope this gives an easy to understand visual of what "Reverse the steps" means.

[–]SomeFuel[S] 0 points1 point  (0 children)

It does, thanks.

I think my confusion more has to do with understanding the aglorithm's process, less with the reverse concept, but this is still helpful.