Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I'm trying to parse a string like this into a List[Byte].
List[Byte]
Here is the string
"0x4e 0x01 0x09"
How can you instantiate a byte from a string representation?
List[Int]
Here's one solution using a regex and parseInt.
parseInt
def parseBytes(s: String): List[Byte] = (raw"\b0x([0-9a-f]{2})\b".r .findAllMatchIn(s) .map(g => Integer.parseInt(g.group(1), 16).toByte) .toList)
Test:
scala> parseBytes("0x4e 0x01 0x09 0xff") 0: List[Byte] = List(78, 1, 9, -1)
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
List[Int].