Purpose

To enable printing of integers in hexadecimal format in Q3 game/bg_lib.c.

Overview

The following conversion specifiers are added to vsprintf() (and thus all other functions based on vsprintf(), such as va() and Com_sprintf()):

Usage

char buf[256];
int i;

i = 999;
vsprintf(buf, "%08X", i);

Result: buf contains "000003E7"

Limitations

Since a signed 32b int is used to hold the value for formatting as unsigned 32b, any value over (2^31)-1 (i.e. 2_147_483_647) will start behaving strangely.

Programming Notes

This patch involved very little change to the existing codebase. I'm surprised formatting as hex hasn't already been added earlier. It's awefully useful for debug-prints of bitmasks/bitflags/flag variables, which is why I added these conversion specifiers to bg_lib.c.

I changed AddInt() into a more generalized function that can convert to any number base. The number base is specified by the character set passed -- the number of characters correlate to the number base. If a character set is not passed, the function will use base-10 (decimal, "human counting"). The original value is continually [integer-wise] divided by the number base and set to the new quotient until zero, with the resulting remainders used as the offset into the character set passed. The algorithm used may seem clever, but it becomes really obvious when you do enough math-and-text-based computer programming.


(2001.08.26)

Patched against Q3A 1.27g game source: q3addhex.diff


-- PhaethonH (PhaethonH@gmail.com)