wangzhibo
2026-08-16 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package nodomain.freeyourgadget.gadgetbridge.test;
 
import org.junit.Assert;
import org.junit.Test;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
 
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
 
import static org.junit.Assert.assertTrue;
 
public class HexToBinaryInputStreamTest extends TestBase {
 
    @Test
    public void testConversion() throws IOException {
        byte[] hexString;
        byte[] binString;
 
        try (InputStream in = HuamiActivityDetailsParserTest.class.getClassLoader().getResourceAsStream("ActivityDetailsDump1.txt")) {
            hexString = FileUtils.readAll(in, 1024 * 1024);
            assertTrue(hexString.length > 1);
            try (InputStream in2 = getContents(HuamiActivityDetailsParserTest.class.getClassLoader().getResource("ActivityDetailsDump1.txt"))) {
                binString = FileUtils.readAll(in2, 1024 * 1024);
                assertTrue(binString.length > 1);
            }
        }
        Assert.assertTrue(hexString.length > binString.length);
        ByteArrayOutputStream binToHexOut = new ByteArrayOutputStream(hexString.length);
        for (int i = 0; i < binString.length; i++) {
            String hexed = String.format("0x%x", binString[i]);
            binToHexOut.write(hexed.getBytes("US-ASCII"));
            if ((i + 1) % 17 == 0) {
                binToHexOut.write('\n');
            } else {
                binToHexOut.write(' ');
            }
        }
 
        byte[] hexedBytes = binToHexOut.toByteArray();
        Assert.assertArrayEquals(hexString, hexedBytes);
    }
 
    private InputStream getContents(URL hexFile) throws IOException {
        return new HexToBinaryInputStream(hexFile.openStream());
    }
 
}