【发布时间】:2015-10-15 12:12:54
【问题描述】:
我有以下字符串及其 Base64 编码版本:
temp = "Last Star Wars 'not for children'\n\nThe sixth and final Star Wars movie may not be suitable for young children, film-maker George Lucas has said.\n\nHe told US TV show 60 Minutes that Revenge of the Sith would be the darkest and most violent of the series. \"I don't think I would take a five or six-year-old to this,\" he told the CBS programme, to be aired on Sunday. Lucas predicted the film would get a US rating advising parents some scenes may be unsuitable for under-13s. It opens in the UK and US on 19 May. He said he expected the film would be classified PG-13 - roughly equivalent to a British 12A rating.\n\nThe five previous Star Wars films have all carried less restrictive PG - parental guidance - ratings in the US. In the UK, they have all been passed U - suitable for all - with the exception of Attack of The Clones, which got a PG rating in 2002. Revenge of the Sith - the third prequel to the original 1977 Star Wars film - chronicles the transformation of the heroic Anakin Skywalker into the evil Darth Vader as he travels to a Hell-like planet composed of erupting volcanoes and molten lava. \"We're going to watch him make a pact with the devil,\" Lucas said. \"The film is much more dark, more emotional. It's much more of a tragedy.\"\n"
temp_enc = "TGFzdCBTdGFyIFdhcnMgJ25vdCBmb3IgY2hpbGRyZW4nXG5cblRoZSBzaXh0aCBhbmQgZmluYWwgU3RhciBXYXJzIG1vdmllIG1heSBub3QgYmUgc3VpdGFibGUgZm9yIHlvdW5nIGNoaWxkcmVuLCBmaWxtLW1ha2VyIEdlb3JnZSBMdWNhcyBoYXMgc2FpZC5cblxuSGUgdG9sZCBVUyBUViBzaG93IDYwIE1pbnV0ZXMgdGhhdCBSZXZlbmdlIG9mIHRoZSBTaXRoIHdvdWxkIGJlIHRoZSBkYXJrZXN0IGFuZCBtb3N0IHZpb2xlbnQgb2YgdGhlIHNlcmllcy4gXCJJIGRvbid0IHRoaW5rIEkgd291bGQgdGFrZSBhIGZpdmUgb3Igc2l4LXllYXItb2xkIHRvIHRoaXMsXCIgaGUgdG9sZCB0aGUgQ0JTIHByb2dyYW1tZSwgdG8gYmUgYWlyZWQgb24gU3VuZGF5LiBMdWNhcyBwcmVkaWN0ZWQgdGhlIGZpbG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VGhlIGZpbG0gaXMgbXVjaCBtb3JlIGRhcmssIG1vcmUgZW1vdGlvbmFsLiBJdCdzIG11Y2ggbW9yZSBvZiBhIHRyYWdlZHkuXCJcbg=="
>>> len(temp)
1251
>>> len(temp_enc)
1688
>>> len(temp)/3
417
>>> (len(temp)/3)*4
1668
字符串的长度可以被 3 整除。既然每 3 个字节我们有 4 个字节的编码,那么为什么编码后的字符串比预期的要长?为什么要在编码中添加填充?
【问题讨论】:
-
你是如何进行编码的?我拿了你的字符串
temp并按照以下方式对其进行编码:import base64temp_enc = base64.b64encode(temp)当我这样做时len(temp_enc)我得到 1668,你的数学显示是正确的值。 -
len(temp_enc)%4 必须为零 ...
标签: python python-2.7 base64