Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting byte array to string and back again [duplicate]

Tags:

scala

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.

like image 985
Lasf Avatar asked Apr 17 '26 23:04

Lasf


1 Answers

UTF-8 uses one-to-four byte unsigned values. You would have to figure out what UTF-8 values you are actually getting when you underflow the values like that.

If you check new String(alpha) == new String(gamma), you will see that it returns true.

like image 135
GreyBeardedGeek Avatar answered Apr 21 '26 15:04

GreyBeardedGeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!