I realize this question is probably idiotic, but hey, rough day. Anyway, given this:
scala> import java.nio.charset.Charset
import java.nio.charset.Charset
scala> val alpha = Array[Byte](2,-9,-7,-126,-36,-41,-16,56)
alpha: Array[Byte] = Array(2, -9, -7, -126, -36, -41, -16, 56)
scala> val beta = new String(alpha, Charset.forName("UTF-8"))
beta: String = ?������8
scala> val gamma = beta.getBytes(Charset.forName("UTF-8"))
gamma: Array[Byte] = Array(2, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, 56)
Why doesn't alpha == gamma? What's the correct way to do this?
Update: I see Base64 encoding/decoding works. But I am still interested in why UTF-8 doesn't. Perhaps it's because there is no UTF-8 representation of one or more those bytes.