integer - Convert hexadecimal file into unsigned int 32 Big-Endian in Java -
it might question asked, have not found satisfactory reply yet out there. in particular because conversion has been done in c or c++.
btw, how convert hexadecimal file (200mb) uint32 big-endian representation in java?
this illustration of trying achieve:
54 00 00 00 -> 84 55 f1 2e 04 -> 70185301 a2 3f 32 01 -> 20070306 , on
edit
file fileinputstring = new file(inputfilefield.gettext()); fileinputstream fin = new fileinputstream(fileinputstring); fileoutputstream out = new fileoutputstream(filedirectoryfolder.gettext() +"/"+ fileinputstring.getname()); byte[] filecontent = new byte[(int)fileinputstring.length()]; fin.read(filecontent); system.out.println("file lenght" + filecontent.length); for(int = 0; < filecontent.length; i++){ byte b = filecontent[i]; // boxing conversion converts `byte` `byte` int value = b.intvalue(); out.write(value); } close(); system.out.println("done");
edit 2
file fileinputstring = new file(inputfilefield.gettext()); fileinputstream fin = new fileinputstream(fileinputstring); fileoutputstream out = new fileoutputstream(filedirectoryfolder.gettext() +"/"+ fileinputstring.getname()); bytearrayoutputstream bos = new bytearrayoutputstream(); byte[] filecontent = new byte[(int)fileinputstring.length()]; system.out.println("file lenght" + filecontent.length); int bytesread; while (( bytesread = fin.read(filecontent)) != -1) { bytebuffer.wrap(filecontent).order(byteorder.little_endian).getlong(); bos.write(filecontent, 0, bytesread); } out.write(bos.tobytearray()); system.out.println("done");
edit 3
dataoutputstream out = new dataoutputstream(new fileoutputstream(output)); datainputstream in = new datainputstream(new fileinputstream(input))) { int count = 0; while (count < input.length() - 4) { in.readfully(buffer, 4, 4); string s=long.tostring(bytebuffer.wrap(buffer).order(byteorder.little_endian).getlong()); out.writebytes( s + " "); count += 4; }
thanks
the next code should suffice. uses long
values ensure can represent range of positive values 4 bytes can represent.
note: code assumes hex input 4 bytes. may want add together more checks , measures in production code.
private static long tolong(string hex) { hex = hex.replace(" ", "") + "00000000"; byte[] info = datatypeconverter.parsehexbinary(hex); homecoming bytebuffer.wrap(data).order(byteorder.little_endian).getlong(); } public static void main(string[] args) throws exception { system.out.println(tolong("54 00 00 00")); system.out.println(tolong("55 f1 2e 04")); system.out.println(tolong("a2 3f 32 01")); system.out.println(tolong("ff ff ff ff")); }
output:
84 70185301 20070306 4294967295based on recent edits, propose code such following. note assumes input multiple of 4 bytes in length. left-over bytes ignored:
file input = new file("whatever"); byte[] buffer = new byte[8]; list<long> result = new arraylist<>(); seek (datainputstream in = new datainputstream(new fileinputstream(input))) { int count = 0; // note: trailing bytes ignored while (count < input.length() - 4) { in.readfully(buffer, 4, 4); result.add(bytebuffer.wrap(buffer) .order(byteorder.little_endian).getlong()); count += 4; } }
java integer big-endian
No comments:
Post a Comment