猿问

如何将文件中的十六进制值读入字节数组?

我有一个文件,其中包含注释(看起来像以双斜杠开头的 Java 单行注释//)和以空格分隔的十六进制值。


文件如下所示:


//create applet instance

0x80 0xB8 0x00 0x00 0x0c 0x0a 0xa0 0x00 0x00 0x00 0x62 0x03 0x01 0xc 0x01 0x01 0x00 0x7F;

如何将十六进制值从字符串转换为字节数组的行?


我使用以下方法:


List<byte[]> commands = new ArrayList<>();

Scanner fileReader = new Scanner(new FileReader(file));

while (fileReader.hasNextLine()) {

      String line = fileReader.nextLine();

      if (line.startsWith("0x")) {

          commands.add(line.getBytes());

      }

}

但可以肯定的是,这显示了符号的字节表示,因为它们是字符并且不会将其转换为字节。这是正确的。但是如何正确转换呢?


幕布斯6054654
浏览 131回答 2
2回答

当年话下

你在正确的轨道上。只需删除尾随;,然后使用课程中为您提供的方法Integer。while ( fileReader.hasNextLine() ) {&nbsp; &nbsp; String line = fileReader.nextLine();&nbsp; &nbsp; if ( line.startsWith( "0x" ) ) {&nbsp; &nbsp; &nbsp; &nbsp; line = line.replace( ";", "" );&nbsp; &nbsp; &nbsp; &nbsp; List<Byte> wrapped = Arrays&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .asList( line.split( " " ) )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // convert all the string representations to their Int value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map( Integer::decode )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // convert all the Integer values to their byte value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map( Integer::byteValue )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect( Collectors.toList() );&nbsp; &nbsp; &nbsp; &nbsp; // if you're OK with changing commands to a List<Byte[]>, you can skip this step&nbsp; &nbsp; &nbsp; &nbsp; byte[] toAdd = new byte[wrapped.size()];&nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i < toAdd.length; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toAdd[i] = wrapped.get( i );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; commands.add( toAdd );&nbsp; &nbsp; }}

德玛西亚99

只是想我会指出,如果你稍微放松一下规范,你基本上可以在一行中做到这一点splitAsStream。List<Integer> out = Pattern.compile( "[\\s;]+" ).splitAsStream( line )&nbsp; &nbsp; &nbsp; &nbsp;.map( Integer::decode ).collect( Collectors.toList() );我在这里使用整数,Integer::decode因为会在 OP 的第一个输入Byte::decode上引发错误。0x80如果你真的需要一个原语数组,你就必须做更多的工作,但实际上装箱的数字通常会做。这是整个代码:public class ScannerStream {&nbsp; &nbsp;static String testVector = "//create applet instance\n" +"0x80 0xB8 0x00 0x00 0x0c 0x0a 0xa0 0x00 0x00 0x00 0x62 0x03 0x01 0xc 0x01 0x01 0x00 0x7F;";&nbsp; &nbsp;public static void main( String[] args ) {&nbsp; &nbsp; &nbsp; List<List<Integer>> commands = new ArrayList<>();&nbsp; &nbsp; &nbsp; Scanner fileReader = new Scanner( new StringReader( testVector ) );&nbsp; &nbsp; &nbsp; while( fileReader.hasNextLine() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String line = fileReader.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if( line.startsWith( "0x" ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Integer> out = Pattern.compile( "[\\s;]+" ).splitAsStream( line )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map( Integer::decode ).collect( Collectors.toList() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( out );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; commands.add( out );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println( commands );&nbsp; &nbsp;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答