Updating hex dump to better handle 7F/FF bytes for display. #54

This commit is contained in:
Rob Greene 2022-01-09 21:49:36 -06:00
parent f0c83d35fe
commit a5714ed019
2 changed files with 28 additions and 11 deletions

View File

@ -43,12 +43,6 @@ public class AppleUtil {
*/
private static final int BYTES_PER_LINE = 16;
/**
* This is the ASCII space character as used by the Apple ][.
* The high bit is off.
*/
private static final int APPLE_SPACE = 0x20;
/**
* Bit masks used for the bit shifting or testing operations.
*/
@ -582,11 +576,11 @@ public class AppleUtil {
int index = offset+a;
if (index < bytes.length) {
char ch = (char) (bytes[index] & 0x7f);
if ((byte)ch >= (byte)APPLE_SPACE) {
printer.print(ch);
} else {
printer.print('.');
}
if (Character.isISOControl(ch)) {
printer.print('.');
} else {
printer.print(ch);
}
} else {
printer.print(' ');
}

View File

@ -19,6 +19,7 @@
*/
package com.webcodepro.applecommander.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -113,4 +114,26 @@ public class AppleUtilTest {
// Confirm that these disks are identical:
assertTrue(AppleUtil.disksEqualByBlock(prodosDiskDosOrder, prodosDiskNibbleOrder));
}
@Test
public void testGetHexDump_expectedWidth() {
final int expectedWidth = 76;
// Setup an array of all byte values
byte[] data = new byte[256];
for (int i=0; i<data.length; i++) {
data[i] = (byte)i;
}
// Ensure the width is expected and there are no control characters
String result = AppleUtil.getHexDump(data);
String[] lines = result.split("\n");
for (String line : lines) {
System.out.println(line);
if (line.startsWith("$")) {
assertEquals(expectedWidth, line.length());
line.chars().forEach(i -> assertTrue(Integer.toString(i), i >= ' '));
}
}
}
}