javax. xml. Bind’s Base64 encoder / decoder eats the last two characters of the string

I need to convert some strings using Base64 encoding, and I'm glad to see that I don't have to scroll through my own Converter - Java provides a javax xml. bind. DataConverter. However, it has some problems This is my time output using Jython repl:

>>> import javax.xml.bind.DatatypeConverter as DC
>>> import java.lang.String as String
>>> def foo(text):
...   return DC.printBase64Binary(DC.parseBase64Binary(String(text)))
... 
>>> foo("hello")
'hell'
>>> foo("This,it's a punctuated sentence.")
'Thisitsapunctuatedsenten'
>>> foo("\"foo\" \"bar\"")
'foob'
>>> foo("\"foo\" \"bar\"12")
'foobar12'
>>> foo("\"foo\" \"bar\"1")
'foob'

As you can see, it doesn't handle non alphanumeric characters at all and often – but not always – truncates strings by two characters

I think it's probably time to write my own class, but now I'm worried a) I didn't read Javadoc or something, b) the class didn't work as expected

So any help is very grateful; Thank you in advance

Solution

Hello is not a Base64 string, so parsing failed You must convert the string to a byte array (try string (text) GetBytes ('utf-8 '), and then call DC in the byte array Printbase64binary() to get the data in Base64

Then DC Parsebase64binary() converts this Base64 encoded data back into a byte array (you can then convert it back into a string)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>