vrbilgi
vrbilgi

Reputation: 5803

Base64 Decoding of UTF 8 character and printing it

I have UTF8 character which are encoded in Base64. I am decoding it and trying to print it.But its only priting its hex value but not the UTF-8 String.

Things I tried

1: I have verified the Character is online decoder which tell me that the string I get is of right format: https://www.base64decode.org/

2: System.getProperty("file.encoding") is also UTF8

def main(args: Array[String]): Unit = {
    println("Hello, world!")

    val sd = Base64.getDecoder.decode("7IiY7KeE7IiY7KeE7IiY7KeE7IiY7KeE7IiY7KeE7IiY7KeE7IiY7KeE7IiY7KeE7IiY")
    println(System.getProperty("file.encoding"))
    println(sd.toString())

    val sb = new StringBuffer

    var i:Int=0

    while(i<sd.length){
      sb.append("%02X".format(sd(i) & 0xff))

      i=i+1

    }
    println(sb)

  }

What am I missing in this one ? Actruall string required is as below:

수진수진수진수진수진수진수진수진수

Upvotes: 2

Views: 1620

Answers (2)

scas
scas

Reputation: 229

Why not simply using new String(sd, "UTF-8"), which will return your characters. Worked on my machine, result: 수진수진수진수진수진수진수진수진수

Upvotes: 2

kliew
kliew

Reputation: 3183

The X in "%02X".format(sd(i) & 0xff) specifies upper case hexadecimal. Try %s to get the UTF-8 string.

Upvotes: 0

Related Questions