Java 如何将十六进制值从文件读入字节数组?
我有一个由注释(类似于以双斜杠开始的Java单行注释,Java 如何将十六进制值从文件读入字节数组?,java,java.util.scanner,filereader,Java,Java.util.scanner,Filereader,我有一个由注释(类似于以双斜杠开始的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<
/
)和空格分隔的十六进制值组成的文件
文件如下所示:
//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());
}
}
List命令=new ArrayList();
Scanner fileReader=new Scanner(new fileReader(file));
while(fileReader.hasNextLine()){
String line=fileReader.nextLine();
if(第行开始时带有(“0x”)){
add(line.getBytes());
}
}
但肯定的是,这显示了符号的字节表示,因为它们是字符,而不是将其转换为字节。这是正确的。但如何正确地转换呢
提前谢谢。你走对了方向。只需删除尾随的
然后使用Integer
类中为您提供的方法
while ( fileReader.hasNextLine() ) {
String line = fileReader.nextLine();
if ( line.startsWith( "0x" ) ) {
line = line.replace( ";", "" );
List<Byte> wrapped = Arrays
.asList( line.split( " " ) )
.stream()
// convert all the string representations to their Int value
.map( Integer::decode )
// convert all the Integer values to their byte value
.map( Integer::byteValue )
.collect( Collectors.toList() );
// if you're OK with changing commands to a List<Byte[]>, you can skip this step
byte[] toAdd = new byte[wrapped.size()];
for ( int i = 0; i < toAdd.length; i++ ) {
toAdd[i] = wrapped.get( i );
}
commands.add( toAdd );
}
}
while(fileReader.hasNextLine()){
String line=fileReader.nextLine();
if(第行开始时带有(“0x”)){
行=行。替换(“;”,“”);
列表包装=数组
.asList(第.split行(“”)
.stream()
//将所有字符串表示形式转换为其Int值
.map(整数::解码)
//将所有整数值转换为字节值
.map(整数::字节值)
.collect(Collectors.toList());
//如果可以将命令更改为列表,则可以跳过此步骤
byte[]toAdd=新字节[wrapped.size()];
for(int i=0;i
我只是想指出,如果稍微放松一下规范,基本上可以使用splitAsStream
在一行中完成这项工作
List<Integer> out = Pattern.compile( "[\\s;]+" ).splitAsStream( line )
.map( Integer::decode ).collect( Collectors.toList() );
你是在纸上还是在脑子里?我会忽略这个问题;在行尾,我将在空格上拆分字符串,忽略0x前缀,并将每个十六进制数解析为一个字节。这也是你的程序需要做的。作为旁白,它将解析十六进制字符串。它不会解析
0x
,所以你需要去掉那部分,还有空格。是的,我也想到了这一点。但是我认为Java有一些解析包含十六进制值的字符串的util方法。@markspace您可以使用Integer::decode
,它将根据前缀为您选择基数。@mypetlion为什么不使用Byte::decode
?Java.lang.Byte还有一个decode
方法,这样您就不用映射了(整数::字节值)
@DodgyCodeException谢谢你的提示。我没有意识到这一点。我已经更新了答案。@DodgyCodeException回到了我以前的答案,因为我意识到Byte::decode
会对OP的一些示例数据抛出错误。是的,我还遇到了使用Byte::decode
的错误。顺便说一句,这是一个很好的解决方案。
public class ScannerStream {
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;";
public static void main( String[] args ) {
List<List<Integer>> commands = new ArrayList<>();
Scanner fileReader = new Scanner( new StringReader( testVector ) );
while( fileReader.hasNextLine() ) {
String line = fileReader.nextLine();
if( line.startsWith( "0x" ) ) {
List<Integer> out = Pattern.compile( "[\\s;]+" ).splitAsStream( line )
.map( Integer::decode ).collect( Collectors.toList() );
System.out.println( out );
commands.add( out );
}
}
System.out.println( commands );
}
}