mirror of
https://codeberg.org/sethm/symon.git
synced 2026-11-07 19:12:23 +00:00
Faster byte and word to hex string
I was alarmed to discover just how slow `String.format()` is for doing integer to hex conversions. Now that a trace window has been added to Symon, it became especially clear that I needed a more efficient way to handle it. I looked into using `Integer.toHexString()`, but I would have had to wrap it to do zero-padding, so I decided to just bite the bullet and do my own with a lookup table. The implementation in `HexUtil` is just as fast as `Integer.toHexString()`, but also zero-pads appropriately. Combined with Java's `+` String concatenation, it seems perfectly adequate. For yet better performance with the trace window, it would make a lot of sense to special-case stepping so that it just pops the top line off the log, and appends to the bottom, rather than re-stringifying the entire trace log each time. This will be a future enhancement.
This commit is contained in:
1 parent
bc3de80892
commit
807a43ce6f
5 files changed
+225
-56
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package com.loomcom.symon;
|
||||
|
||||
import com.loomcom.symon.util.HexUtil;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class HexUtilTest extends TestCase {
|
||||
public void testByteToHex() {
|
||||
assertEquals("FE", HexUtil.byteToHex(0xfe));
|
||||
assertEquals("00", HexUtil.byteToHex(0));
|
||||
assertEquals("0A", HexUtil.byteToHex(10));
|
||||
}
|
||||
|
||||
public void testByteToHexIgnoresSign() {
|
||||
assertEquals("FF", HexUtil.byteToHex(-1));
|
||||
}
|
||||
|
||||
public void testByteToHexMasksLowByte() {
|
||||
assertEquals("FE", HexUtil.byteToHex(0xfffe));
|
||||
assertEquals("00", HexUtil.byteToHex(0xff00));
|
||||
}
|
||||
|
||||
public void testWordToHex() {
|
||||
assertEquals("0000", HexUtil.wordToHex(0));
|
||||
assertEquals("FFFF", HexUtil.wordToHex(65535));
|
||||
assertEquals("FFFE", HexUtil.wordToHex(65534));
|
||||
}
|
||||
|
||||
public void testWordToHexIgnoresSign() {
|
||||
assertEquals("FFFF", HexUtil.wordToHex(-1));
|
||||
}
|
||||
|
||||
public void testWordToHexMasksTwoLowBytes() {
|
||||
assertEquals("FFFE", HexUtil.wordToHex(0xfffffe));
|
||||
assertEquals("FF00", HexUtil.wordToHex(0xffff00));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user