2 years ago

#44194

test-img

user3685285

How can I undo this Base64 encoding? (Scala)

I have a piece of code which does some encoding like so:

def byteBufferToString(b: ByteBuffer): String = java.util.Base64.getEncoder.encodeToString(bb.array)

I'd also like another method which does the opposite. Here's a few of my attempts:

def stringToByteBuffer(str: String): ByteBuffer = ByteBuffer.wrap(Base64.getDecoder.decode(str))

def stringToByteBuffer(str: String): ByteBuffer = ByteBuffer.wrap(str.getBytes(Charset.forName("UTF-8"))

def stringToByteBuffer(str: String): ByteBuffer = ByteBuffer.wrap(Base64.getDecoder.decode(str.getBytes(Charset.forName("UTF-8")))

But none of these seems to satisfy the condition:

byteBufferToString(stringToByteBuffer(testString)) == testString

For example, when testString is abc, I get something like = abc= instead. Any ideas?

string

scala

encoding

base64

bytebuffer

0 Answers

Your Answer

Accepted video resources