【发布时间】:2018-05-04 09:12:24
【问题描述】:
我正在寻找一种从文件中逐行读取十六进制字符串并将它们作为转换后的字节附加到某个 ByteBuffer 的方法。
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
Files.lines(filePath).foreach( l ->
byteBuffer.put(
// first of all strip newlines and normalize string
l.replaceAll("/\n|\r/g", "").toUpperCase()
// but what to do here?
// is there something like
// take next 2 characters (-> Consumer)
// and replace them with the converted byte?
// E.g. "C8" -> 0xC8
// until the end of the string is reached
)
);
这已经被回答了一百万次了。但我想知道是否有使用 Files.lines() 返回的流的解决方案。
一般我喜欢this 的回答。谁能帮我把它翻译成基于 java-8 流的解决方案或从上面完成我的示例?
谢谢!
【问题讨论】:
-
链接的答案与十六进制字符串解析有什么关系?
标签: java java-8 java-stream string-conversion