+ nibbles = bits >> 2;
+ switch (base) {
+
+ case SC_HEX:
+ for (counter = 0; counter < nibbles; ++counter)
+ *(--pos) = "0123456789abcdef"[_val(val[counter])];
+
+ /* last nibble must be masked */
+ if (bits & 3) {
+ x = and_table[_val(val[++counter])][bits & 3];
+ *(--pos) = "0123456789abcdef"[_val(x)];
+ }
+
+ /* now kill zeros */
+ for (; counter > 1; --counter, ++pos)
+ if (pos[0] != '0')
+ break;
+ break;
+
+ case SC_BIN:
+ for (counter = 0; counter < nibbles; ++counter) {
+ pos -= 4;
+ p = binary_table[_val(val[counter])];
+ pos[0] = p[0];
+ pos[1] = p[1];
+ pos[2] = p[2];
+ pos[3] = p[3];
+ }
+
+ /* last nibble must be masked */
+ if (bits & 3) {
+ x = and_table[_val(val[++counter])][bits & 3];
+
+ pos -= 4;
+ p = binary_table[_val(x)];
+ pos[0] = p[0];
+ pos[1] = p[1];
+ pos[2] = p[2];
+ pos[3] = p[3];
+ }
+
+ /* now kill zeros */
+ for (counter <<= 2; counter > 1; --counter, ++pos)
+ if (pos[0] != '0')
+ break;
+ break;
+
+ case SC_DEC:
+ case SC_OCT:
+ memset(base_val, SC_0, CALC_BUFFER_SIZE);
+ base_val[0] = base == SC_DEC ? SC_A : SC_8;
+
+ /* transfer data into oscilating buffers */
+ memset(div1_res, SC_0, CALC_BUFFER_SIZE);
+ for (counter = 0; counter < nibbles; ++counter)
+ div1_res[counter] = val[counter];
+
+ /* last nibble must be masked */
+ if (bits & 3) {
+ ++counter;
+
+ div1_res[counter] = and_table[_val(val[counter])][bits & 3];
+ }
+
+ m = div1_res;
+ n = div2_res;
+ for (;;) {
+ _divmod(m, base_val, n, rem_res);
+ t = m;
+ m = n;
+ n = t;
+ *(--pos) = "0123456789abcdef"[_val(rem_res[0])];
+
+ x = 0;
+ for (i = 0; i < sizeof(div1_res); ++i)
+ x |= _val(m[i]);
+
+ if (x == 0)
+ break;
+ }
+ break;
+
+ default:
+ assert(0);
+ return NULL;
+}