0

I have an Array[Byte] and I want to convert it to an Array[Int]:

for example,

val x : Array[Byte] = Array(192.toByte, 168.toByte, 1.toByte, 9.toByte)

val y : Array[Int] = Array(192, 168, 1, 9)

How can I convert x to y ?

3 Answers 3

2

You can use simply map

val y:Array[Int] = x.map(_.toInt)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

val y = x.map(_.toInt)

Comments

0

I tried this and it works:

val y = x.map(_.toInt & 0xff).deep

2 Comments

This will do and retain the values as they are I checked, but gives return type of IndexedSeq[Any] Can you please explian those & 0xff and .deep. Arwa Z?
When I do simply val y = x.map(_.toInt) it returns I@50930bff, and when I do val y = x.map(_.toInt).deep it returns Array(-64, -88, 1, 9). That is why I add & 0xff so the result is Array(192, 168, 1, 9).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.