convert a byte array to string

Viewed 53784

My Scala code received a binary from byte stream,it looks like [61 62 63 64].The content is "abcd". I use toString to convert it p, but failed. How do I print it as string ?

3 Answers

The bytes to string function I was looking for was where each byte was just a numeric value represented as a string, without any implied encoding. Thanks to the suggestions supplied here, I ended up with the following function which works for my purposes. I post it here incase it useful to someone else.

  def showBytes(bytes: Array[Byte]):String = {
    bytes.map(b => "" + b.toInt).mkString(" ")
  }

This function will return a string containing numeric values separated by spaces.

Related