Termiraptor
Termiraptor

Reputation: 23

Can you convert an array of Bytes to an array of Strings and then back to an array of Bytes?

Let's say I have an array of Bytes

val base = [-2, 50, 48]

and I converted this array of Bytes to an array of Strings

val baseConverted = ["-2","50","48"]

Is there a way to convert 'baseConverted' back to 'base'?

Upvotes: 2

Views: 50

Answers (2)

Valy Dia
Valy Dia

Reputation: 2851

Here is how one can do it using Array:

val baseConverted = Array("-2", "50", "48")

val backToBase = baseConverted.map(_.toByte) 

// Prints
// backToBase: Array[Byte] = Array(-2, 50, 48)

Upvotes: 0

Krzysztof Atłasik
Krzysztof Atłasik

Reputation: 22595

You just need to use toByte from a String on every element:

List("-2","50","48").map(_.toByte)

Upvotes: 3

Related Questions