Reputation: 8711
I have the output of char2hex() from a database column which is a string of hex characters like "41424320202020200A20". I need to check if this hex sequence contains any control character i.e between ascii range \x00 to \x1F (Line feed, Form feed, Carriage return, etc).
I can check like
scala> val x = "41424320202020200A20"
x: String = 41424320202020200A20
scala> x.contains("0A")
res107: Boolean = true
scala> x.indexOf("0A")
res109: Int = 16
scala>
but I need to ensure that "0A" always matches at the odd position(s) in my string x, so that the hex sequence is correctly checked.
I can combine them like
scala> x.indexOf("0A") %2==0 && x.contains("0A")
res111: Boolean = true
scala>
But that would stop with only the first occurrence of the control character.
How do I get all the control chars from the hex string?.
Example:- If I have the string like "41424320202020200A200B000C"
then my output should be List("0A","0B","00","0C")
Upvotes: 2
Views: 81
Reputation: 626
Thank you for providing a desired output, in this this code it's res
:
val x = "41424320202020200A200B000C"
val controlCharacters = (0x00 to 0x1F).map(c => f"$c%02X".toUpperCase)
val res = (x.toUpperCase.grouped(2).toSeq intersect controlCharacters).toList
EDIT: I've put the control character range you've mentioned in a single solution.
Hope it helps.
Upvotes: 2