将字节字符串解析为本机scala字节
我正试图将这样的字符串解析为将字节字符串解析为本机scala字节,scala,Scala,我正试图将这样的字符串解析为列表[字节] 这是绳子 "0x4e 0x01 0x09" 如何从字符串表示中实例化字节?这里有一个使用正则表达式和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)
列表[字节]
这是绳子
"0x4e 0x01 0x09"
如何从字符串表示中实例化字节?这里有一个使用正则表达式和
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)
测试:
这里有一个使用正则表达式和
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)
测试:
该示例的预期输出是什么?List(0x4e,0x010000000,0x09)这是一个
List[Int]
。您是对的,简化操作以使用其他数据。该示例的预期输出是什么?List(0x4e,0x010000000,0x09)这是一个List[Int]
。您是对的,简化操作以使用其他数据。