Python Forum
i need some help using this code below
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need some help using this code below
#1
#From this code im suppose to create base64 for encoding and decoding, im new using pyhton and i need this for my school proecjt, can u tell me how to do that 


def bin2hex(bin_value):
    '''
    >>> bin2hex ('1111')
    'f'
    '''
    return (hex(int(bin_value,2))[2:])
def hex2bin(hex_value):
    '''
    >>> hex2bin ('f')
    '1111'
    '''
    return (bin(int(hex_value,16))[2:])
# targeti ne forme binary ==> z.fill (perdorimi i funksionit) ==> modulo 8 bit
def fillupbyte(binary):
    '''
    >>> fillupbyte('011')
    '00000011'
    >>> fillupbyte('11100111')
    '11100111'
    >>> fillupbyte('1010')
    '00001010'
    >>> fillupbyte('101010')
    '00101010'
    '''
    target_length = len(binary) + (8 - len(binary) % 8) % 8
    return binary.zfill(target_length)
# padet ME BITE NGA E DJATHTA (separate 6 bits)
base64_table ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

def binary2base64(binary):
    padded_binary = binary.ljust(len(binary) + (6 - len(binary) % 6) % 6, '0')
    ret = ""
    while len(padded_binary) > 0:
        actual_bit_sequence = padded_binary[:6]
        padded_binary = padded_binary[6:]
        v = int(actual_bit_sequence, 2)
        ret += base64_table[v]
    while len(ret) % 4 != 0:
        ret += "="
    return ret
def int2base64(value):
    '''
    >>> int2base64(0x61)
    'YQ=='
    >>> int2base64(0x78)
    'eA=='
    '''

def hex2base64(hex_value):
    '''
    >>> hex2base64('61')
        'YQ=='
    >>> hex2base64('123456789abcde')
        'EjRWeJq83g=='
    >>> hex2base64('7368726f6f6d')
        'c2hyb29t'
    '''
buran write Nov-26-2020, 06:27 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
And the question is?
Reply


Forum Jump:

User Panel Messages

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