0

Here's a function that manually converts an integer to any base between 2 and 36, the parameter list is:

void toBase(
   int value,		// Integer value to convert
   char *target,	// Pointer to a large enough buffer
   int base,		// Base (from 2 to 36)
   int fixedDigitCount	// The minimum number of digits it must contain
);

This gives me the following output:

Binary with no fixed number of digits (1 to 15):
1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

Binary with 4 fixed digits (1 to 15):
0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111


Octal with no fixed number of digits (1 to 15):
1 2 3 4 5 6 7 10 11 12 13 14 15 16 17

Octal with 3 fixed digits (1 to 15):
001 002 003 004 005 006 007 010 011 012 013 014 015 016 017


Decimal with no fixed number of digits (1 to 15):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Decimal with 4 fixed digits (1 to 15):
0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015


Hex with no fixed number of digits (1 to 15):
1 2 3 4 5 6 7 8 9 A B C D E F

Hex with 2 fixed digits (1 to 15):
01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

Edited by William Hemsworth: n/a

#include <iostream>
#include <iomanip>
using namespace std;

const char *valLookup = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void toBase(int value, char *target, int base, int fixedDigitCount = 0) {
  int digitCount = 1;
  int temp = value;
  bool _signed = value < 0;

  // Find digit count
  while ( temp /= base )
    digitCount++;

  // Compare with fixeed number of digits, use highest
  int i = max( fixedDigitCount, digitCount ) + _signed;

  // Add sign
  if ( _signed )
    target[0] = '-';

  // Convert
  for (int off; i - _signed; i--) {
    off = (value % base);
    value /= base;
    target[i-1] = valLookup[off < 0 ? -off : off];
  }

  // Add null-ptr
  target[ max(fixedDigitCount, digitCount) + _signed ] = 0;
}

int main() {
  char buffer[32];

  cout << "Binary with no fixed number of digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 2 );
    cout << buffer << ' ';
  }

  cout << "\n\nBinary with 4 fixed digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 2, 4 );
    cout << buffer << ' ';
  }


  cout << "\n\n\nOctal with no fixed number of digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 8 );
    cout << buffer << ' ';
  }

  cout << "\n\nOctal with 3 fixed digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 8, 3 );
    cout << buffer << ' ';
  }


  cout << "\n\n\nDecimal with no fixed number of digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 10 );
    cout << buffer << ' ';
  }

  cout << "\n\nDecimal with 4 fixed digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 10, 4 );
    cout << buffer << ' ';
  }

  cout << "\n\n\nHex with no fixed number of digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 16 );
    cout << buffer << ' ';
  }

  cout << "\n\nHex with 2 fixed digits (1 to 15):\n";
  for (int i = 1; i <= 15; ++i) {
    toBase( i, buffer, 16, 2 );
    cout << buffer << ' ';
  }

  cin.ignore();
}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by William Hemsworth
0

I had never even heard of a negative base until you said that, I looked it up and it seems interesting. But, this function already uses a sign to represent negative numbers, maybe I could make a different one to handle negative bases.

Thanks for the feedback :)

Edited by William Hemsworth: n/a

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.