我想读取一个32字节块的文件和每个块我希望将它作为HEX表示而不启动0x。
到目前为止,这是我的代码:
FileChannel inChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
Log.i("info", "mappedByteBuffer " + buffer.toString());
byte[] buffer_ = new byte[32];
int len;
while ((len = inChannel.read(buffer)) > 0) {
Log.i("info", "reading chunk len " + len);
}
为了转换为HEX,我建议:
public static String bytesToHex(int bytes) {
int i = bytes & 0xFF;
return Integer.toHexString(i);
}
但我的问题是我总是得到相同的十六进制。当然,我需要为每个块提供不同的HEX值。
答案 0 :(得分:0)
在调试代码时,我遇到了多个问题。首先是我在while循环表达式中得到IllegalArgumentException
。我也没有看到你在哪里调用方法bytesToHex
,或者当你使用一个字节数组时它为什么需要一个int
参数。
鉴于上述情况,我写了这个:
我编写了这个方法,它接受一个字节数组并将其转换为十六进制字符串:
public static String bytesToHex(byte[] in) {
final StringBuilder builder = new StringBuilder();
for (byte b : in) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
使用此代码对其进行测试:
public static void main(String[] args) throws IOException {
File inputFile = new File("text.txt");
InputStream stream = new FileInputStream(inputFile);
byte[] buffer = new byte[32];
int len;
int pos = 0;
while ((len = stream.read(buffer)) > 0) {
System.out.println(String.format("Position: %s, length: %s, hex: %s", pos, len, bytesToHex(buffer)));
pos++;
}
}
这是整个文件:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws IOException {
File inputFile = new File("text.txt");
InputStream stream = new FileInputStream(inputFile);
byte[] buffer = new byte[32];
int len;
int pos = 0;
while ((len = stream.read(buffer)) > 0) {
System.out.println(String.format("Position: %s, length: %s, hex: %s", pos, len, bytesToHex(buffer)));
pos++;
}
}
public static String bytesToHex(byte[] in) {
final StringBuilder builder = new StringBuilder();
for (byte b : in) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
}
上面的代码可以做你想要的。
在测试时,我在文件test.txt
Ignorant branched humanity led now marianne too strongly entrance. Rose to shew bore no ye of paid rent form. Old design are dinner better nearer silent excuse. She which are maids boy sense her shade. Considered reasonable we affronting on expression in. So cordial anxious mr delight. Shot his has must wish from sell nay. Remark fat set why are sudden depend change entire wanted. Performed remainder attending led fat residence far.
Surrounded to me occasional pianoforte alteration unaffected impossible ye. For saw half than cold. Pretty merits waited six talked pulled you. Conduct replied off led whether any shortly why arrived adapted. Numerous ladyship so raillery humoured goodness received an. So narrow formal length my highly longer afford oh. Tall neat he make or at dull ye.
Ignorant saw her her drawings marriage laughter. Case oh an that or away sigh do here upon. Acuteness you exquisite ourselves now end forfeited. Enquire ye without it garrets up himself. Interest our nor received followed was. Cultivated an up solicitude mr unpleasant.
Article nor prepare chicken you him now. Shy merits say advice ten before lovers innate add. She cordially behaviour can attempted estimable. Trees delay fancy noise manor do as an small. Felicity now law securing breeding likewise extended and. Roused either who favour why ham.
Rooms oh fully taken by worse do. Points afraid but may end law lasted. Was out laughter raptures returned outweigh. Luckily cheered colonel me do we attacks on highest enabled. Tried law yet style child. Bore of true of no be deal. Frequently sufficient in be unaffected. The furnished she concluded depending procuring concealed.
Admiration we surrounded possession frequently he. Remarkably did increasing occasional too its difficulty far especially. Known tiled but sorry joy balls. Bed sudden manner indeed fat now feebly. Face do with in need of wife paid that be. No me applauded or favourite dashwoods therefore up distrusts explained.
Difficulty on insensible reasonable in. From as went he they. Preference themselves me as thoroughly partiality considered on in estimating. Middletons acceptance discovered projecting so is so or. In or attachment inquietude remarkably comparison at an. Is surrounded prosperous stimulated am me discretion expression. But truth being state can she china widow. Occasional preference fat remarkably now projecting uncommonly dissimilar. Sentiments projection particular companions interested do at my delightful. Listening newspaper in advantage frankness to concluded unwilling.
Allow miles wound place the leave had. To sitting subject no improve studied limited. Ye indulgence unreserved connection alteration appearance my an astonished. Up as seen sent make he they of. Her raising and himself pasture believe females. Fancy she stuff after aware merit small his. Charmed esteems luckily age out.
Affronting discretion as do is announcing. Now months esteem oppose nearer enable too six. She numerous unlocked you perceive speedily. Affixed offence spirits or ye of offices between. Real on shot it were four an as. Absolute bachelor rendered six nay you juvenile. Vanity entire an chatty to.
An country demesne message it. Bachelor domestic extended doubtful as concerns at. Morning prudent removal an letters by. On could my in order never it. Or excited certain sixteen it to parties colonel. Depending conveying direction has led immediate. Law gate her well bed life feet seen rent. On nature or no except it sussex.
它产生了这些结果:
Position: 0, length: 32, hex: 49676e6f72616e74206272616e636865642068756d616e697479206c6564206e
Position: 1, length: 32, hex: 6f77206d617269616e6e6520746f6f207374726f6e676c7920656e7472616e63
Position: 2, length: 32, hex: 652e20526f736520746f207368657720626f7265206e6f207965206f66207061
Position: 3, length: 32, hex: 69642072656e7420666f726d2e204f6c642064657369676e206172652064696e
Position: 4, length: 32, hex: 6e657220626574746572206e65617265722073696c656e74206578637573652e
Position: 5, length: 32, hex: 2053686520776869636820617265206d6169647320626f792073656e73652068
Position: 6, length: 32, hex: 65722073686164652e20436f6e7369646572656420726561736f6e61626c6520
Position: 7, length: 32, hex: 776520616666726f6e74696e67206f6e2065787072657373696f6e20696e2e20
Position: 8, length: 32, hex: 536f20636f726469616c20616e78696f7573206d722064656c696768742e2053
Position: 9, length: 32, hex: 686f742068697320686173206d75737420776973682066726f6d2073656c6c20
Position: 10, length: 32, hex: 6e61792e2052656d61726b206661742073657420776879206172652073756464
Position: 11, length: 32, hex: 656e20646570656e64206368616e676520656e746972652077616e7465642e20
Position: 12, length: 32, hex: 506572666f726d65642072656d61696e64657220617474656e64696e67206c65
Position: 13, length: 32, hex: 6420666174207265736964656e6365206661722e200d0a0d0a537572726f756e
Position: 14, length: 32, hex: 64656420746f206d65206f63636173696f6e616c207069616e6f666f72746520
Position: 15, length: 32, hex: 616c7465726174696f6e20756e616666656374656420696d706f737369626c65
Position: 16, length: 32, hex: 2079652e20466f72207361772068616c66207468616e20636f6c642e20507265
Position: 17, length: 32, hex: 747479206d657269747320776169746564207369782074616c6b65642070756c
Position: 18, length: 32, hex: 6c656420796f752e20436f6e64756374207265706c696564206f6666206c6564
Position: 19, length: 32, hex: 207768657468657220616e792073686f72746c79207768792061727269766564
Position: 20, length: 32, hex: 20616461707465642e204e756d65726f7573206c6164797368697020736f2072
Position: 21, length: 32, hex: 61696c6c6572792068756d6f7572656420676f6f646e65737320726563656976
Position: 22, length: 32, hex: 656420616e2e20536f206e6172726f7720666f726d616c206c656e677468206d
Position: 23, length: 32, hex: 7920686967686c79206c6f6e676572206166666f7264206f682e2054616c6c20
Position: 24, length: 32, hex: 6e656174206865206d616b65206f722061742064756c6c2079652e200d0a0d0a
Position: 25, length: 32, hex: 49676e6f72616e742073617720686572206865722064726177696e6773206d61
Position: 26, length: 32, hex: 727269616765206c617567687465722e2043617365206f6820616e2074686174
Position: 27, length: 32, hex: 206f722061776179207369676820646f20686572652075706f6e2e2041637574
Position: 28, length: 32, hex: 656e65737320796f7520657871756973697465206f757273656c766573206e6f
Position: 29, length: 32, hex: 7720656e6420666f726665697465642e20456e71756972652079652077697468
Position: 30, length: 32, hex: 6f757420697420676172726574732075702068696d73656c662e20496e746572
Position: 31, length: 32, hex: 657374206f7572206e6f7220726563656976656420666f6c6c6f776564207761
Position: 32, length: 32, hex: 732e2043756c7469766174656420616e20757020736f6c69636974756465206d
Position: 33, length: 32, hex: 7220756e706c656173616e742e200d0a0d0a41727469636c65206e6f72207072
Position: 34, length: 32, hex: 657061726520636869636b656e20796f752068696d206e6f772e20536879206d
Position: 35, length: 32, hex: 657269747320736179206164766963652074656e206265666f7265206c6f7665
Position: 36, length: 32, hex: 727320696e6e617465206164642e2053686520636f726469616c6c7920626568
Position: 37, length: 32, hex: 6176696f75722063616e20617474656d7074656420657374696d61626c652e20
Position: 38, length: 32, hex: 54726565732064656c61792066616e6379206e6f697365206d616e6f7220646f
Position: 39, length: 32, hex: 20617320616e20736d616c6c2e2046656c6963697479206e6f77206c61772073
Position: 40, length: 32, hex: 65637572696e67206272656564696e67206c696b657769736520657874656e64
Position: 41, length: 32, hex: 656420616e642e20526f75736564206569746865722077686f206661766f7572
Position: 42, length: 32, hex: 207768792068616d2e200d0a0d0a526f6f6d73206f682066756c6c792074616b
Position: 43, length: 32, hex: 656e20627920776f72736520646f2e20506f696e747320616672616964206275
Position: 44, length: 32, hex: 74206d617920656e64206c6177206c61737465642e20576173206f7574206c61
Position: 45, length: 32, hex: 7567687465722072617074757265732072657475726e6564206f757477656967
Position: 46, length: 32, hex: 682e204c75636b696c79206368656572656420636f6c6f6e656c206d6520646f
Position: 47, length: 32, hex: 2077652061747461636b73206f6e206869676865737420656e61626c65642e20
Position: 48, length: 32, hex: 5472696564206c617720796574207374796c65206368696c642e20426f726520
Position: 49, length: 32, hex: 6f662074727565206f66206e6f206265206465616c2e204672657175656e746c
Position: 50, length: 32, hex: 792073756666696369656e7420696e20626520756e61666665637465642e2054
Position: 51, length: 32, hex: 6865206675726e69736865642073686520636f6e636c7564656420646570656e
Position: 52, length: 32, hex: 64696e672070726f637572696e6720636f6e6365616c65642e200d0a0d0a4164
Position: 53, length: 32, hex: 6d69726174696f6e20776520737572726f756e64656420706f7373657373696f
Position: 54, length: 32, hex: 6e206672657175656e746c792068652e2052656d61726b61626c792064696420
Position: 55, length: 32, hex: 696e6372656173696e67206f63636173696f6e616c20746f6f20697473206469
Position: 56, length: 32, hex: 66666963756c74792066617220657370656369616c6c792e204b6e6f776e2074
Position: 57, length: 32, hex: 696c65642062757420736f727279206a6f792062616c6c732e20426564207375
Position: 58, length: 32, hex: 6464656e206d616e6e657220696e6465656420666174206e6f7720666565626c
Position: 59, length: 32, hex: 792e204661636520646f207769746820696e206e656564206f66207769666520
Position: 60, length: 32, hex: 7061696420746861742062652e204e6f206d65206170706c6175646564206f72
Position: 61, length: 32, hex: 206661766f75726974652064617368776f6f6473207468657265666f72652075
Position: 62, length: 32, hex: 7020646973747275737473206578706c61696e65642e200d0a0d0a4469666669
Position: 63, length: 32, hex: 63756c7479206f6e20696e73656e7369626c6520726561736f6e61626c652069
Position: 64, length: 32, hex: 6e2e2046726f6d2061732077656e7420686520746865792e2050726566657265
Position: 65, length: 32, hex: 6e6365207468656d73656c766573206d652061732074686f726f7567686c7920
Position: 66, length: 32, hex: 7061727469616c69747920636f6e73696465726564206f6e20696e2065737469
Position: 67, length: 32, hex: 6d6174696e672e204d6964646c65746f6e7320616363657074616e6365206469
Position: 68, length: 32, hex: 73636f76657265642070726f6a656374696e6720736f20697320736f206f722e
Position: 69, length: 32, hex: 20496e206f72206174746163686d656e7420696e71756965747564652072656d
Position: 70, length: 32, hex: 61726b61626c7920636f6d70617269736f6e20617420616e2e20497320737572
Position: 71, length: 32, hex: 726f756e6465642070726f737065726f7573207374696d756c6174656420616d
Position: 72, length: 32, hex: 206d652064697363726574696f6e2065787072657373696f6e2e204275742074
Position: 73, length: 32, hex: 72757468206265696e672073746174652063616e20736865206368696e612077
Position: 74, length: 32, hex: 69646f772e204f63636173696f6e616c20707265666572656e63652066617420
Position: 75, length: 32, hex: 72656d61726b61626c79206e6f772070726f6a656374696e6720756e636f6d6d
Position: 76, length: 32, hex: 6f6e6c792064697373696d696c61722e2053656e74696d656e74732070726f6a
Position: 77, length: 32, hex: 656374696f6e20706172746963756c617220636f6d70616e696f6e7320696e74
Position: 78, length: 32, hex: 6572657374656420646f206174206d792064656c6967687466756c2e204c6973
Position: 79, length: 32, hex: 74656e696e67206e657773706170657220696e20616476616e74616765206672
Position: 80, length: 32, hex: 616e6b6e65737320746f20636f6e636c7564656420756e77696c6c696e672e20
Position: 81, length: 32, hex: 0d0a0d0a416c6c6f77206d696c657320776f756e6420706c6163652074686520
Position: 82, length: 32, hex: 6c65617665206861642e20546f2073697474696e67207375626a656374206e6f
Position: 83, length: 32, hex: 20696d70726f76652073747564696564206c696d697465642e20596520696e64
Position: 84, length: 32, hex: 756c67656e636520756e726573657276656420636f6e6e656374696f6e20616c
Position: 85, length: 32, hex: 7465726174696f6e20617070656172616e6365206d7920616e206173746f6e69
Position: 86, length: 32, hex: 736865642e205570206173207365656e2073656e74206d616b65206865207468
Position: 87, length: 32, hex: 6579206f662e204865722072616973696e6720616e642068696d73656c662070
Position: 88, length: 32, hex: 6173747572652062656c696576652066656d616c65732e2046616e6379207368
Position: 89, length: 32, hex: 65207374756666206166746572206177617265206d6572697420736d616c6c20
Position: 90, length: 32, hex: 6869732e20436861726d65642065737465656d73206c75636b696c7920616765
Position: 91, length: 32, hex: 206f75742e200d0a0d0a416666726f6e74696e672064697363726574696f6e20
Position: 92, length: 32, hex: 617320646f20697320616e6e6f756e63696e672e204e6f77206d6f6e74687320
Position: 93, length: 32, hex: 65737465656d206f70706f7365206e656172657220656e61626c6520746f6f20
Position: 94, length: 32, hex: 7369782e20536865206e756d65726f757320756e6c6f636b656420796f752070
Position: 95, length: 32, hex: 65726365697665207370656564696c792e2041666669786564206f6666656e63
Position: 96, length: 32, hex: 652073706972697473206f72207965206f66206f666669636573206265747765
Position: 97, length: 32, hex: 656e2e205265616c206f6e2073686f74206974207765726520666f757220616e
Position: 98, length: 32, hex: 2061732e204162736f6c7574652062616368656c6f722072656e646572656420
Position: 99, length: 32, hex: 736978206e617920796f75206a7576656e696c652e2056616e69747920656e74
Position: 100, length: 32, hex: 69726520616e2063686174747920746f2e200d0a0d0a416e20636f756e747279
Position: 101, length: 32, hex: 2064656d65736e65206d6573736167652069742e2042616368656c6f7220646f
Position: 102, length: 32, hex: 6d657374696320657874656e64656420646f75627466756c20617320636f6e63
Position: 103, length: 32, hex: 65726e732061742e204d6f726e696e672070727564656e742072656d6f76616c
Position: 104, length: 32, hex: 20616e206c6574746572732062792e204f6e20636f756c64206d7920696e206f
Position: 105, length: 32, hex: 72646572206e657665722069742e204f72206578636974656420636572746169
Position: 106, length: 32, hex: 6e207369787465656e20697420746f207061727469657320636f6c6f6e656c2e
Position: 107, length: 32, hex: 20446570656e64696e6720636f6e766579696e6720646972656374696f6e2068
Position: 108, length: 32, hex: 6173206c656420696d6d6564696174652e204c61772067617465206865722077
Position: 109, length: 32, hex: 656c6c20626564206c6966652066656574207365656e2072656e742e204f6e20
Position: 110, length: 32, hex: 6e6174757265206f72206e6f20657863657074206974207375737365782e200d
Position: 111, length: 3, hex: 0a0d0a757265206f72206e6f20657863657074206974207375737365782e200d