How do I add character padding to a string?

心已入冬 提交于 2019-12-11 18:27:15

问题


Okay this is very hard to explain, but i want to add padding to a sentence so that the characters in that sentence are a multiple of n.

however this number '4' has to change to 3 and 5 so it has to work then as well..

anyone know what im talking about?? and how to do it?


回答1:


I hope the self commented code below would help you grasp the concept. You just have to do some maths to get the pad characters at either end

Some concept

  1. Extra Characters required Padding = len(string) % block_length
  2. Total_Pad_Characters = block_length - len(string) % block_length
  3. Pad Character's at front = Total_Pad_Characters/2
  4. Pad Character's at end = Total_Pad_Characters - Total_Pad_Characters/2

So here is the code

>>> def encrypt(st,length):
    #Reversed the String and replace all Spaces with 'X'
    st = st[::-1].replace(' ','X')
    #Find no of characters to be padded.
    padlength = (length - len(st)%length) % length
    #Pad the Characters at either end
    st = 'X'*(padlength/2)+st+'X'*(padlength-padlength/2)
    #Split it with size length and then join with a single space
    return ' '.join(st[i:i+length] for i in xrange(0,len(st),length))

>>> encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) #Your Example
'XECN ELIG IVXL ANRE TEXS IXMO DEER FXFO XECI RPXE HTXX'
>>> encrypt('THE PRICE', 5) # One Extra Character at end for Odd Numbers
'ECIRP XEHTX'
>>> encrypt('THE PRIC', 5) # 1 Pad Characters at either end
'XCIRP XEHTX'
>>> encrypt('THE PRI', 5) # 1 Pad Characters at either end and one Extra for being Odd
'XIRPX EHTXX'
>>> encrypt('THE PR', 5) # 2 Pad Characters at either end
'XXRPX EHTXX'
>>> encrypt('THE P', 5) # No Pad characters required
'PXEHT'
>>> encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 5) #Ashwini's Example
'XXECN ELIGI VXLAN RETEX SIXMO DEERF XFOXE CIRPX EHTXX'
>>>



回答2:


>>> import math
>>> def encrypt(string, length):
        inverse_string = string.replace(' ','X')[::-1]

        center_width = int(math.ceil(len(inverse_string)/float(length)) * length) # Calculate nearest multiple of length rounded up

        inverse_string = inverse_string.center(center_width,'X')

        create_blocks = ' '.join(inverse_string[i:i+length] for i in xrange(0,len(inverse_string),length))
        return create_blocks

>>> encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4)
'XECN ELIG IVXL ANRE TEXS IXMO DEER FXFO XECI RPXE HTXX'



回答3:


def pad(yourString,blockLength):
    return yourString + ("X" * (blockLength - (len(yourString) % blockLength)))

Is your padding function. for end padding. If you need center padding use:

def centerPad(yourString,blockLength):
    return ("X" * ((blockLength - (len(yourString) % blockLength))/2)) + yourString + ("X" * ((blockLength - (len(yourString) % blockLength))/2))

you need to take a harder look at the rest of your code if you want to implement a block cypher.



来源:https://stackoverflow.com/questions/10070857/how-do-i-add-character-padding-to-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!