Question

I'm working on a specialty hexadecimal editor that includes a Z80 two-byte pointer converter.

The mathematics behind the conversion are like so:

  1. Take the offset that you wish to point to.
  2. Take the last four digits of the offset, and cut off the rest.
  3. If the offset is outside the range &H4000 - &H7FFF, it must be converted like this: (offset % &H4000) + &H4000. In other words:
    • If the offset is from &H0000 to &H3FFF, add &H4000 to the offset.
    • If the offset is from &H4000 to &H7FFF, do not do anything to the offset.
    • If the offset is from &H8000 to &HBFFF, subtract &H4000 from the offset.
    • If the offset is from &HC000 to &HFFFF, subtract &H8000 from the offset.

My problem is I don't know how I could turn a 5 or 6-digit hex offset into a two-digit offset. How would I shave off the extra bytes at the beginning (step two)?

Was it helpful?

Solution

With the "remainder" operator, spelled Mod in Visual Basic:

offset Mod &H10000

OTHER TIPS

The answer using the 'Mod' operator is correct.

But, to be pedantic, this is a 'modulus' operator, and not a 'remainder' operator. There is a difference for negative numbers. (I appreciate we are not talking about negative numbers here.)

See What's the difference between “mod” and “remainder” ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top